Practice13 - Structures - reminder typedef struct Graduate...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Structures - reminder typedef struct Graduate { int final_grade; char ID[SIZE_OF_ID]; int year_of_graduation; }Graduate; Structures more examples typedef struct Grade { int score; char distinction[SIZE_OF_STRING]; } Grade; Structures more examples typedef struct Graduate { Grade final_grade; char ID[SIZE_OF_ID]; int year_of_graduation; }Graduate; Structures more examples int main( void ) { Graduate grad1; scanf("%d",&grad1.final_grade.score); if (grad1.final_grade.score>80 && grad1.final_grade.score<=90) strcpy(grad1.final_grade.distinction,"Dean's honors"); if (grad1.final_grade.score>90) strcpy(grad1.final_grade.distinction,"President's honors"); scanf("%s",grad1.ID); scanf("%d",&grad1.year_of_graduation); printf("ID: %s graduated at %d\n",grad1.ID,grad1.year_of_graduation); if (grad1.final_grade.score>80) printf("%s\n",grad1.final_grade.distinction); return 0; } Structures containing arrays A structure member that is an array does not behave like an ordinary array When copying a structure that contains a member which is an array, the array is copied element by element Not just the address gets copied For example - array_member.c Reminder ordinary arrays cant be copied simply by using the = operator They must be copied using a loop Structures containing arrays The same happens when passing the structure to a function Changing the array inside the function wont change it in the calling function Reminder when passing an ordinary array to a function, all that gets passed is the address of its first element Hence every change to the array within the function, changes the array in the calling function Pointers are another matter If the member is a pointer, for example to a dynamically allocated array, all that gets copied is the pointer (the address) itself For example, pointer_member.c Hence, we should take extra care when manipulating structures that contain pointers Linked lists - reminder typedef struct Student_t { char ID[ID_LENGTH]; char Name[NAME_LENGTH]; int grade; struct Student_t *next; /* A pointer to the next item on the list */ } Student; Creating a new kind of student Student *create_student( char name, char ID, int grade) { Student *std; std = (Student *)malloc( sizeof (Student)); if (std == NULL) { printf(Memory allocation error!\n); exit(1); } strcpy(std->Name, name); strcpy(std->ID, ID); std->grade = grade; std->next = NULL; return std; } A function for adding a student Student *add_student(Student *head, Student *to_add) { /*to_add points to the student that we want to add */ Student *next_std, *previous_std=NULL;...
View Full Document

Page1 / 54

Practice13 - Structures - reminder typedef struct Graduate...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online