Download presentation
Presentation is loading. Please wait.
1
1 TDBA66, vt-04, Lecture Ch9 Type declaration Possible to create user defined data types Used to give special names for a certain data type typedef data type type_name; Ex. typedef unsigned int PosInt;... PosInt randomNumber;
2
2 TDBA66, vt-04, Lecture Ch9 example... typedef double ** MatrixType; void writeMatrix(MatrixType matrix, int m, int n); /* prototype */ void main(void) { MatrixType a; int i,j; a = (double *) calloc(4,sizeof(double *)); for (i=0;i<4;i++) a[i] = (double *) calloc(3,sizeof(double)); srand(time(NULL)); /* random seed */ for (i=0;i<4;i++) for (j=0;j<3;j++) a[i][j] = rand()%100 - 50.0; writeMatrix(a,4,3); }/* main */... Prototype
3
3 TDBA66, vt-04, Lecture Ch9 Data structures We have used arrays where every element of the same data type assemble information of different data types in one unit (a record) Example: you want to organize your books One book is described by oTitle oAuthor oYear published oIf borrowed, by whom oPublisher o…
4
4 TDBA66, vt-04, Lecture Ch9 New data structure in C- struct struct tagname { members /* of different data types */ }; Variables can now be declared struct tagname var1,var2,var3; tagname may be omitted - but in that case you have not given a name to the struct that can not be used to declare variables
5
5 TDBA66, vt-04, Lecture Ch9 BookType typedef struct Book { char *title; int publishedYear; char *author; }BookType; struct Book can be used to declare variables struct Book b; BookType othervar;
6
6 TDBA66, vt-04, Lecture Ch9 Book – without tagname struct { char *title; int publishedYear; char *author; }b1,b2,b3; b1,b2 and b3 variables of type above No further variables can be declared (unless repeating) Impossible for parameter declaration
7
7 TDBA66, vt-04, Lecture Ch9 The members To access a certain member in a variable of type struct one uses dot notation struct_variable.member a member can be treated like a single variable of the same data type. Assume BookType b, otherBook; b.title = "Bilbo - en hobbits äventyr"; b.author = "J.J.R. Tolkien"; b.publishedYear = 1935; A variable of type struct can be assigned the value of an other variable of the same struct BookType b, otherBook; /* b gets values as above */ otherBook = b; /* all information is copied */
8
8 TDBA66, vt-04, Lecture Ch9 Example Book #include typedef struct { char *title; int publishedYear; char *author; } BookType; int main(void) { BookType b; /* not needed !!!!!!!!!b.title = malloc(25*sizeof(char)); b.author = malloc(25*sizeof(char)); */ b.title = "Bilbo - en hobbits äventyr"; b.author = "J.J.R. Tolkien"; b.publishedYear= 1935; printf("Title : %s \n",b.title); printf("Author: %s \n",b.author); printf("Published: %d\n",b.publishedYear); return 0; }/* main */
9
9 TDBA66, vt-04, Lecture Ch9 Pointers & structures Pointer to struct is, of course, useful when a struct is an output parameter from a function Ex. BookType *p; *p is the struct that p points to In the Book-example int main(void) { BookType *p,b; p = &b; (*p).title = malloc(25*sizeof(char)); (*p).author = malloc(25*sizeof(char));
10
10 TDBA66, vt-04, Lecture Ch9 the operator -> Instead of the dot operator acting on a certain struct we can use the operator -> directly on a pointer to the struct (indirect component selection operator) Ex. (*p).title p->title The example ones again typedef struct { char *title; int publishedYear; char *author; } BookType; int main(void) { BookType *p,b ; p = &b; p->title = malloc(25*sizeof(char)); p->author = malloc(25*sizeof(char));
11
11 TDBA66, vt-04, Lecture Ch9 example Book pointer #include typedef struct { char *title; int publishedYear; char *author; } BookType; int main(void) { BookType *b,c; b=&c; (*b).title = "Bilbo - en hobbits äventyr"; b->author = "J.J.R. Tolkien"; (*b).publishedYear= 1935; printf("Title : %s \n",b->title); printf("Author: %s \n",b->author);/* c.author */ printf("Published: %d\n",b->publishedYear); return 0; }/* main */
12
12 TDBA66, vt-04, Lecture Ch9 example BookList #include typedef struct { char *title; int publishedYear; char *author; } BookType; void writeLibrary(BookType *, int); int main(void) { BookType *list; int i; int n = 3; list = calloc(50,sizeof(Book)); for (i=0; i <= n-1;i++) { list[i].title = (char *)calloc(25,sizeof(char)); list[i].author = (char *)calloc(25,sizeof(char)); }
13
13 TDBA66, vt-04, Lecture Ch9 cont.………………… … … … …... BookList Printf("\nType info. of your books\n"); for (i=0; i <= n-1;i++) { printf("Title[%d] : ",i); fgets(list[i].title, 25, stdin); fflush(stdin); printf("Author : "); fgets(list[i].author, 25, stdin); fflush(stdin); printf("Publishing year: "); scanf("%d",&list[i].publishedYear); getchar(); } writeLibrary(list,n); return 0; }
14
14 TDBA66, vt-04, Lecture Ch9... writeLibrary void writeLibrary(BookType *bib, int no_of) { int i; for (i=0; i <= no_of-1;i++) { printf("Book #%d\n",i); printf("Title : %s\n",bib[i].title); printf("Author : %s\n",bib[i].author); printf("Publishing year : %d\n\n",bib[i].publishedYear); } }/* writeLibrary */
15
15 TDBA66, vt-04, Lecture Ch9 /* * Gets and returns a Book structure */ BookType get_book(void){ BookType *book_p; book_p = (BookType *)calloc(1,sizeof(BookType); book_p->title = (char *) calloc(25,sizeof(char)); book_p->author = (char *) calloc(25,sizeof(char)); printf("Title:”); fgets(book_p->title, 25, stdin); printf("Author: ”); fgets(book_p->author,25, stdin); printf("Publishing year: "); scanf("%d",&b.publishedYear); getchar(); fflush(stdin); return (*book_p); }/* end of get_book */ Function can return a struct
16
16 TDBA66, vt-04, Lecture Ch9 BookList (version 2) #include typedef struct { char *title; int publishedYear; char *author; } BookType; void writeLibrary(BookType *, int); BookType get_book(void); int main(void) { BookType *list; int i; int n = 3; list = (BookType *) calloc(50,sizeof(Book)); for (i=0; i <= n-1;i++) list[i]=get_book(); writeLibrary(list,n); return 0; }
17
17 TDBA66, vt-04, Lecture Ch9 peppar:~/c/Ckod>./bok2 Title:Differential Equations Author: Davis Publishing year: 1995 Title:Numerical Linear Algebra and Optimization Author: Gill, Murray, Wright Publishing year: 1990 Title:Matrix Computations Author: Golub, van Loan Publishing year: 1988 Book #0 Title : Differential Equations Author : Davis Publishing year : 1995 Book #1 Title : Numerical Linear Algebra Author : Gill, Murray, Wright Publishing year : 1990 Book #2 Title : Matrix Computations Author : Golub, van Loan Publishing year : 1988
18
18 TDBA66, vt-04, Lecture Ch9 #include Extras struct dept { char avd[50]; float man_lon; long anst_nr; }; struct retired { float pension; int slut_ar; }; typedef struct { char namn[80]; char persnr[10]; char telnr[20]; char jobbkod; union{/* one of these should be active */ struct dept arbetare; struct retired PROman; } variant; } pers_post;
19
19 TDBA66, vt-04, Lecture Ch9 void main(void) pers_post p1, p2, p3; printf("\nType name:"); scanf("%s", p1.namn); fflush(stdin); printf("\nGiven name is= %s", p1.namn); printf("\nType job code (A or P):"); scanf("%c", &p1.jobbkod); fflush(stdin); printf("\nGiven joc code is= %c\n", p1.jobbkod); switch (p1.jobbkod){ case 'a': case 'A’: printf("\nTyep name od dept.:"); scanf ("%s",p1.variant.arbetare.avd); fflush(stdin); /* and so on */ break; case 'p': case 'P': printf("\nType pension per year: "); scanf("%f", &p1.variant.PROman.pension ); fflush(stdio); printf(“Given pension is= %10.2f\n”, p1.variant.PROman.pension); /* and so on */ break; } p2=p1; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.