01000001) 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)
curly brackets at beginning and end Semicolon at end of each line Semicolon at the end of the structure struct student{ char *first; char last[MAX]; int age; double gpa; };
Initialize the structure with the “structure member operator” (.) Also called the “dot operator” struct student s3; s3.first=“Sally”; strncpy(s3.last, “Suzuki”, MAX); s3.age = 33; s3.gpa = 3.33;
pointer operator” (->) Also called the “arrow operator” Can also dereference the pointer (*) printf(“Name is: %s %s\nAge is: %d\nGPA is: %f\n\n”, ptr->first, (*ptr).last, ptr->age, (*ptr).gpa);
multiplied by the number of array elements is the number of bytes in memory char b[10]; int d[10]; double f[10]; printf(“%d”,sizeof(b)); //10 printf(“%d”,sizeof(d)); //40 printf(“%d”,sizeof(f)); //80
the number of bytes for that data type is returned char b[10], *p1 = b; int d[10], *p2 = d; double f[10], *p3 = f; printf(“%d”,sizeof(*p1)); //1 printf(“%d”,sizeof(*p2)); //4 printf(“%d”,sizeof(*p3)); //8
padding”, because computers may store specific data types on certain memory boundaries struct student s1 = {“S”,“D”,11,1.1}; struct student s2; printf(“%d”,sizeof(s1)); //40 printf(“%d”,sizeof(s2)); //40
four bytes For arrays of structures, multiply the structure bytes by the number of array elements struct student *s3 = &s2; struct student s4[10]; printf(“%d”,sizeof(s3)); //4 printf(“%d”,sizeof(s4)); //400
of a structure, so any changes to the structure inside the function, do not change the structure outside the function birthday1(student1); //code. . . void birthday1(struct student s){ s.age++; }
of a structure’s address, so any changes to the structure inside the function, do change the structure outside function birthday2(&student1); //code. . . void birthday2(struct student *s){ s->age++; }
of a structure to a function, in this case pass by value, so no changes to original structure member age birthday3(student1.age); //code. . . void birthday3(int age){ age++; }
member of a structure to a function, using pass by reference, so does change the original structure member age birthday4(&student1.age); //code. . . void birthday4(int *age){ (*age)++; }
examples of pass by value and pass by reference for a whole structure and an individual structure member % cp structfun.c program.c % make –f makefile-program
create a new data type name, which is used as a synonym in our code We can use the new data type name anywhere in our code in place of the regular data type name Syntax: typedef datatype NewDataTypeName;
a new type Instead adds a new name for an exiting type Similar to #define, except it is interpreted by the compiler Student s3; s3.first=“Sally”; strncpy(s3.last, “Suzuki”, MAX); s3.age = 33; s3.gpa = 3.33; printStudent(s3);
shuffling a deck of cards using structures and typedef by initializing, shuffling, and displaying the card deck % cp cards.c program.c % make –f makefile-program