Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = 01000001) Field –Group of characters (character string “Fred”)

Similar presentations


Presentation on theme: "C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = 01000001) Field –Group of characters (character string “Fred”)"— Presentation transcript:

1 C Structures Basics of structures Typedef

2 Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”) Record –Composed of related fields (struct) File –Group of related records (student record file) Database –Group of related files (students, faculty, and staff files)

3 Structure Declaration Structure –Collection of related variables Structure tag –“student” Structure’s members –first, last, age, gpa struct student{ char first[20]; char *last; int age; double gpa; };

4 Structure Definition Define & initialize with list of variables Define & initialize using the dot operator (structure member operator) struct student s1 = {"Ted","Tanaka", 22, 2.22}; struct student s2; strcpy(s2.first,"Sally"); s2.last="Suzuki"; s2.age = 33; s2.gpa = 3.33;

5 Accessing Members Structure member operator, or dot operator –For direct variables (.) Structure pointer operator –For pointers (->) struct student s2; struct student *sPtr =&s2; printf("%s %d\n", s2.last, s2.age); /*Tanaka 22*/ printf("%s %d\n", (*sPtr).last, sPtr->age); /*Suzuki 33*/ *See complete program at accessingMembers.caccessingMembers.c

6 sizeof Operator Returns the size in bytes of a data type char a, b[10]; int c, d[10]; double e, f[10]; printf("sizeof(a) = %d\n",sizeof(a)); printf("sizeof(b) = %d\n",sizeof(b));... /* sizeof(a) = 1 sizeof(b) = 10 */ /* sizeof(c) = 4 sizeof(d) = 40 */ /* sizeof(e) = 8 sizeof(f) = 80 */

7 sizeof Operator Examples with pointers char b[10], *p1 = b; int d[10], *p2 = d; double f[10], *p3 = f; printf("sizeof(p1) = %d ",sizeof(p1)); printf("sizeof(*p1) = %d\n",sizeof(*p1));... /* sizeof(p1) = 4 sizeof(*p1) = 1 */ /* sizeof(p2) = 4 sizeof(*p2) = 4 */ /* sizeof(p3) = 4 sizeof(*p3) = 8 */

8 sizeof Operator Examples with structures struct student s1={"Ted","Tanaka", 22,2.22}; struct student s2; struct student *s3 = &s2; struct student s4[10]; printf("sizeof(s1) = %d\n", sizeof(s1));... /*sizeof(s1) = 40 sizeof(s2) = 40 sizeof(s3) = 4 sizeof(s4) = 400 */

9 sizeof Operator struct student{ char first[20]; /* 20 bytes */ char *last; /* 4 bytes */ int age; /* 4 bytes */ double gpa; /* 8 bytes */ };/*total = 36 bytes ?? */ Structures may have “extra padding”, because computers may store specific data types on certain memory boundaries –See complete program at sizeof.csizeof.c

10 Structures & Functions You can pass a structure to a function by value struct student s1={"Ted","Tanaka", 22, 2.22}; incAge1(s1); printStudent(s1); /* Name = Ted Tanaka age = 22 gpa = 2.22 */ void incAge1(struct student s){s.age++;} void printStudent(struct student s){ printf("Name = %s %s \n age = %d gpa = %.2f\n",s.first, s.last, s.age, s.gpa);}

11 Structures & Functions Or pass by reference (pointer to a structure) struct student s1={"Ted","Tanaka", 22, 2.22}; struct student *p = &s1; incAge2(p); printStudent(s1); /* Name = Ted Tanaka age = 23 gpa = 2.22 */ void incAge2(struct student *s){s->age++;} –See complete program at functions.cfunctions.c

12 Arrays of Structures Can also have an array of a structure main(){ struct student s[100]={"","", 0, 0.0}; printStudent(s[0]); } /* Name = age = 0 gpa = 0.00 */

13 struct A{/*See struct1.c*/struct1.c char b; int c; }; struct A fun(struct A); main(){ struct A d = {'e', 71}; /* sizeof(d)=8 */ struct A e[20]; printf("sizeof(e)=%d\n", sizeof(e)); e[0] = fun(d); printf("b=%c c=%d\n", d.b, d.c); printf("b=%c c=%d\n", e->b, (*e).c); } struct A fun(struct A a){ printf("b=%c c=%d\n", a.b++, ++a.c); return a;}

14 typedef Creates new data type name –Integer used as a synonym for int –IntegerPtr used as synonym for int * (See program at typedef.c)typedef.c typedef int Integer; typedef int* IntegerPtr; main(){ Integer a = 10; IntegerPtr b = &a; a++; printf("a = %d\n",a); printf("b = %d\n",*b); } /*a = 11 b = 11*/

15 typedef Creates synonyms for previously declared data types –Can be defined after the struct struct student{ char first[20]; char *last; int age; double gpa; }; typedef struct student Student;

16 typedef Creates synonyms for previously declared data types –Or defined in one statement typedef struct student{ char first[20]; char *last; int age; double gpa; } Student;

17 typedef Does not create a new type Instead adds a new name for an exiting type Similar to #define, except it is interpreted by the compiler Student s; strcpy(s.first,"Fred"); s.last = "Tanaka"; s.age = 22; s.gpa = 2.22;

18 Example Program See fig10_03.c from D & D textbookfig10_03.c –Represents a deck of cards as an array of structures –To shuffle the cards, the program randomly swaps the cards –Output displays 52 cards in two columns –Note the use of the dot operator (.) and the use of typedef


Download ppt "C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = 01000001) Field –Group of characters (character string “Fred”)"

Similar presentations


Ads by Google