More Related Content
More from Dimitris Psounis
More from Dimitris Psounis (20)
Η ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 13 - ΔΟΜΕΣ (ΕΚΤΥΠΩΣΗ)
- 1. H C
13:
.
1.
2.
3.
4.
5.
6.
.
1. !
2.
3.
4. "
5. typedef
#. $ !
1.
2.
.
.
1. ;
!" # $ %
&$ " .
& !" & & ' $ , $ $
$ $ "" # .
( $ $ % ) :
( " & $ & & &.
( & & $ * & % &. ( %
$ & & " & :
+ & ) !" ( $ ) & $ &
& %
+ $ $ & & & simeio ( $ * & ) &
$" !"
, 2 $ " % $ * &
" ' & # &% & ' .
.
2. * &
' ' & , % ' & " " :
" $ " & & :
To struct " ) $" & * "# & .
,-, " % & .
1, 2,..., n !" .
!" # – "# " % & .
struct simeio{
int x;
int y;
};
struct {
1 _ 1;
2 _ 2;
...
n _ _n;
};
- 2. .
3.
O % ' main $ &
& .
% & & , & '
.
& # !" & &
( ' $ % & )
( & ($ main) # "# % & "
" :
+ # ! " " " ( . ) . .:
# "# "" % & ) !" . .
" " :
. $ & ! "
" " ( . ) . .
a.x=10; a.y=5; b.x=4; b.y=9; c.x=12; c.y=18;
struct simeio a;
a.x=5;
a.y=10;
struct simeio a,b,c;
.
3.
/ $ "# & $ "# & $ $
% & & & . . .:
0 & "# & $ $ & "# & $ % &
a,b,c & .
, &"" % !" # , & &
. . . & $
&:
# "# % & , $
$ & !" ! ( . )
struct simeio{
int x;
int y;
} a,b,c;
struct atomo{
char onoma[30];
char eponimo[30];
int ilikia;
float misthos;
};
.
4. $
1 $ % & , ". " " $
" $ # &
/ $ " " $ $ ) :
(& $ $ & % ' &
%$ ).
( % & & atomo $ . . ) :
struct simeio a;
a.x=5;
a.y=10;
struct simeio a = {5,10}
struct atomo sb = {"John","Doe",34,890.44};
.
5. & &
. , % & & , .
2 % & " !" & &
, " !
% % % & * (
$ &' &)
"# % & & % ; "
+ # ! . . x & % & & " :
( " & " .)
!
struct trigono{
struct simeio A;
struct simeio B;
struct simeio C;
};
struct trigono TR;
TR.A.x=5;
- 3. .
6. ( " %
$ " & % ! $ :
"
/* struct.c Deixnei tin vasiki leitourgia mias domis*/
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct person{
char name[80];
char surname[80];
struct date gennisi;
};
( & * …)
.
6. ( " %
#
main()
{
struct person p;
printf("Dwse to onoma: ");
scanf("%s", p.name);
printf("Dwse to eponimo: ");
scanf("%s", p.surname);
printf("Dwse imera gennisis: ");
scanf("%d",&p.gennisi.day);
printf("Dwse mina gennisis: ");
scanf("%d",&p.gennisi.month);
printf("Dwse etos gennisis: ");
scanf("%d",&p.gennisi.year);
printf("n%s %s (%d/%d/%d)", p.name, p.surname,
p.gennisi.day,p.gennisi.month,p.gennisi.year);
}
3. % $ $
1. , " $ #
# & !" $ $
.
( & $ $ & % & "" % & .
) & & % bit bit % ' !
( . . $ " & $# $ :
4 $ & # 10 5 ( " & % $ ! % ' & a
!" b, % ' " :
struct point a;
struct point b;
a.x=10;
a.y=5;
b=a;
printf("%d %d",b.x,b.y);
b.x=a.x;
b.y=a.y;
3. % $ $
2. $ $
+ " $ .
& $ & & % & .
. . & $ ) :
. & 10 !" & struct atomo & !
pinakas[0], pinakas[1],…,pinakas[9], " & !
" " ( . )
. . 1 "
$ 2 " $ 52.
+ # "
$ 5 $ & & 4 & & c
struct atomo pinakas[10];
pinakas[1].ilikia=52;
pinakas[3].onoma[4]='c';
- 4. 3. % $ $
2. $ $
+ & & $ # % $
% .
" sizeof & .
( % " % malloc $ free ) :
struct atomo *pinakas;
pinakas=malloc(sizeof(struct atomo)*10);
if (pinakas==NULL)
{
printf("Adynamia desmeysis mnimis");
exit(0);
}
... pinakas[0],...,pinakas[9]...
free(pinakas);
3. % $ $
2. $ $
%" , $ " $ " % struct_array.c
5 % " '# &.
( & $ $ ' .
5 % ' & $ $ $ '# .
& # ' .
" & # .
3. % $ $
2. $ $
/* struct_array.c: Epeksergasia enos pinaka apo stigmiotupa domis */
#include <stdio.h>
#include <stdlib.h>
#define STRING_SIZE 100
struct epafi {
char name[STRING_SIZE];
char phone[STRING_SIZE];
};
main()
{
struct epafi *pinakas;
int i,N;
/* 1. Eisagwgi tis diastasis toy pinaka */
printf("Dwse plithos epafwn: ");
scanf("%d",&N);
( & * …)
3. % $ $
2. $ $
/* 2. Desmeusi Mnimis */
pinakas=malloc(sizeof(struct epafi)*N);
if (!pinakas)
{
printf("Adynamia desmeusis mnimis");
exit(0);
}
/*3. Diavasma epafwn */
fflush(stdin);
for (i=0; i<N; i++)
{
printf("Dwse onomatepwnimo %d-ou atomou: ",i+1);
gets(pinakas[i].name);
printf("Dwse tilefwno %d-ou atomou: ",i+1);
gets(pinakas[i].phone);
}
( & * …)
- 5. 3. % $ $
2. $ $
/*4. Ektypwsi epafwn */
printf("nnEPAFESn==============");
for (i=0; i<N; i++)
{
printf("n%d) %s (%s) ",i+1,
pinakas[i].name,
pinakas[i].phone);
}
/*5. Apodesmeusi Mnimis */
free(pinakas);
}
3. % $ $
3. $ $
1. $
6 & * & ,
& $ $ & % & .
+ & $ & # & " $ $ .
% & :
+ $ & & $ " & .
& % & . . "
" # % $ .
!
struct minima{
char *p1;
char *p2;
};
struct minima mnm1;
…
p1
…
mnm
p2
3. % $ $
3. $ $
1. $
+ $ % $ & & # % & $ ,
$ "" " " malloc:
$ & $ "" " . .:
4 & $ $ $ "" " # $ $ :
"
…
p1
…
mnm1
p2
mnm1.p1=malloc(10*sizeof(char));
mnm1.p2=malloc(20*sizeof(char));
strcpy(mnm1.p1, "hello");
strcpy(mnm1.p2, "world");
h e l l o 0 …
w o r l d 0 …
3. % $ $
3. $ $
1. $
"# & 2 % & $ $ & $ "" "
" , . .:
1 $ % !" :
#
…
p1
…
mnm2
p2
k a l i m e r a 0 …
k o s m e 0 …
struct minima mnm2;
mnm2.p1=malloc(10*sizeof(char));
mnm2.p2=malloc(20*sizeof(char));
strcpy(mnm2.p1, "kalimera");
strcpy(mnm2.p2, "kosme");
- 6. 3. % $ $
3. $ $
1. $
. & ! % 7 & $ # :
$ " % ' & ! " # mnm1 mnm2,
% bit bit % ' , " $ % ' & &
& . ( $ :
mnm2=mnm1;
…
p1
…
mnm1
p2
h e l l o 0 …
w o r l d 0 …
… …
mnm2
1 & !
p1 p2
3. % $ $
3. $ $
1. $
, % $ & % ' $ & ) % '
"# ) :
$ $ & & .
strcpy(mnm2.p1,mnm1.p1);
strcpy(mnm2.p2,mnm1.p2);
…
p1
…
mnm1
p2
h e l l o 0 …
w o r l d 0 …
… …
mnm2
p1 p2
h e l l o 0 …
w o r l d 0 …
3. % $ $
3. $ $
1. $
" !
• $ & % ' & " $ # , &
$ .
• % , &
' !
3. % $ $
3. $ $
2. $ & &
0 "# & $ & % & , . . " :
( $ !# $ & % & 9)
& ! $ " #
% # ) :
"" $ " ! " $ (% ' " (-) $ % "
( > ), " ( -> ) " ) :
struct atomo x;
struct atomo *p;
p=&x;
(*p).ilikia=35;
p->ilikia=35;
& # ! " $ :
• " " ( . ) % &
• $ $ ( * ) $ " ( . ) $ % & .
• " ! " $ ( -> ) $ % &
- 7. 3. % $ $
4. $ &
3 ! & & & , % & # .
( & & & % & struct
atomo ) :
+ # & $ ' % & & & &
$ * & ' struct atomo.
. # $ & & & & * & .
+ ) & " "" * "
& ' .
void func(struct atomo x)
{
...
}
struct atomo func(int x, int y)
{
...
}
3. % $ $
5. $ " typedef
+ " & ' &
' & $ " ) $" struct.
" typedef $ $ & & & # & %
$ & % $ &" % % .
. . $ " main:
% ' & person % struct atomo. . . "
!" % 7 & :
struct atomo{
char onoma[30];
char eponimo[30];
int ilikia;
float misthos;
};
typedef struct atomo person;
person x,y;
3. % $ $
5. $ " typedef
/ $ « $ » % 7 & " typedef
& .
' & $ & & # & ' & "
typedef:
. ( % $ " & & &
$ $ & . . .:
typedef struct{
char onoma[30];
char eponimo[30];
int ilikia;
float misthos;
} person;
person x,y;
. + #
1.
, # (unions) , "# $
, ' "
$ ' .
+ & & % $ " & " $
' .
% ' " % & $
% $ " ' # .
% " % " & " & & ,
$ " ' & & $ .
!
, # " , " & $ $
" % & " & .
.
- 8. . + #
2. %
"
/* union.c: Deixnei pos xrisimopoiw mia enosi */
#include <stdio.h>
union shared{
char c;
int i;
double d;
};
main()
{
union shared s;
s.c='a';
printf("nXaraktiras: %c", s.c);
printf("nAkeraios : %d", s.i);
printf("nDouble : %f", s.d);
s.d=10.55;
printf("nXaraktiras: %c", s.c);
printf("nAkeraios : %d", s.i);
printf("nDouble : %f", s.d);
}
. $
1. / " % +&$"
#
• 1 ( , ) $ B( , ) :
1. , point " & % &.
2. . $ & & & point, ! * & %
& & $ ' .
3. . $ & & & , & " % * &$"
& $ ' .
4. . $ & & main & & , ! *
& % $ & # &$" & .
& " % % $ * , & sqrt(x)
# ! !" $ & math.h
& " % & %# &, & pow(a,n) & '
an
. $
2. $ &
1. "# " 4 & ! " onoma, diefthinsi, arithmos, nomos. ,
& ! " & % 80
2. " typedef RECORD & # & & .
3. 7 & read_record :
• % & RECORD ' $ " $ " %#
$ "" " $ & .
4. 7 & print_record
• % & RECORD $ & #
& .
5. 7 main & $ & "# $ $ #
% & $ & # .
. $
3. & & $
• % % ' % , #
& $ ( % $ ) & & ! " % &
.
• ( ) " & # " )
• ( & % & $
& )
- 9. . $
4. $ & &
• % % # % $
$ - % & . , % % % -=3 %% ' .
. $
5. & $ $ #
• % % # $ %% '#
& $ . " %% '# % .
. $
6. % ' %
• % % #
1. - % ' %% ' & $ ( " % %% ' )
& % & ( x).
1. & % & ( copy_record) $ "" "
.
2. % & # $ x.
3. " & # " # & $ & $ .