Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-19-structures

 ics212-19-structures

Avatar for William Albritton

William Albritton

November 01, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. What is a Structure?  Related variables grouped under one

    name  Can have different data types in one structure  Used in data structures to create nodes  Used in files to create records
  2. Data Hierarchy  Byte: 8 bits (ASCII character ‘A’ =

    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)
  3. Structure Definition  Start the structure definition  Keyword struct

     Structure tag  student  Structure’s members  first, last, age, gpa struct student{ char *first; char last[MAX]; int age; double gpa; };
  4. Structure Definition Bugs  Don’t forget:  Left and right

    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; };
  5. Initialize Structures with Initializer List  Define and initialize the

    structure with a list of variables struct student s2 = {“Ted”,“Tanaka”, 22, 2.22};
  6. Initialize Structures with Dot Operator  Define a structure 

    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;
  7. Accessing Members with Dot Operator  Use the dot operator

    (.) to access the data members for output printf(“Name is: %s %s\nAge is: %d\nGPA is: %f\n”, s2.first, s2.last, s2.age, s2.gpa);
  8. Accessing Members with Dot Operator  Code output from the

    previous slide Name is: Fred Tanaka Age is: 22 GPA is: 2.220000
  9. Pointer to a Structure  In the pointer declaration, use

    “struct” and the structure’s tag  Also need dereferencing operator (*) and address operator (&) struct student *ptr = &s3;
  10. Accessing Members with Pointers  For pointers, use the “structure

    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);
  11. Example Program  See example code at: structure.c % cp

    structure.c program.c % make –f makefile-program
  12. What is sizeof() Operator?  Returns the number of bytes

    stored in memory char a; int c; double e; printf(“%d”,sizeof(a)); //1 printf(“%d”,sizeof(c)); //4 printf(“%d”,sizeof(e)); //8
  13. Arrays and sizeof() Operator  For arrays, the data type

    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
  14. Pointers and sizeof() Operator  All pointers take up four

    bytes in memory char a, *p1 = &a; int c, *p2 = &c; double e, *p3 = &e; printf(“%d”,sizeof(p1)); //4 printf(“%d”,sizeof(p2)); //4 printf(“%d”,sizeof(p3)); //4
  15. Dereferenced Pointers and sizeof()  If we dereference the pointer,

    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
  16. Structures and sizeof() Operator  With structures, sizeof() operator also

    returns the number of bytes stored in memory struct student{ char *first; // 4 bytes char last[MAX]; //20 bytes int age; // 4 bytes double gpa; // 8 bytes }; //40 bytes total
  17. Structures and sizeof() Operator  However, structures may have “extra

    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
  18. Structures and sizeof() Operator  Pointers to structures take up

    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
  19. Example Code  See example program at: sizeof.c % cp

    sizeof.c program.c % make –f makefile-program
  20. Pass by Value  Pass by value makes a copy

    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++; }
  21. Pass by Reference  Pass by reference makes a copy

    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++; }
  22. Passing Individual Structure Members  Can also pass a member

    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++; }
  23. Passing Individual Structure Members II  Can also pass a

    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)++; }
  24. Example Code  See example program at: structfun.c  Has

    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
  25. Arrays of Structures  Can also have an array of

    a structure struct student array[MAX] = {“”,“”, 0, 0.0}; for(i=0;i<MAX;i++){ array[i].first = “Nemo”; array[i].last[0] = 'A' + i; array[i].age = i; array[i].gpa = i + i/10.0; }
  26. Example Code  See example program at: structarray.c  Has

    examples of an array of a structure % cp structarray.c program.c % make –f makefile-program
  27. What is typedef?  Use typedef can be used to

    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;
  28. Integer Data Type  In this code, “Integer” is used

    in place of “int”  Otherwise, everything remains the same in our code typedef int Integer; //code. . . Integer n = 10; n++; printf(“:%d”,n);
  29. IntegerPointer Data Type  Can also use pointers with typedef

     “IntegerPointer” used in place of “int *” typedef int* IntegerPointer; //code. . . Integer n = 10; IntegerPointer p=&n; printf(“%d”,*p);
  30. Student Data Type  Note that 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 s3; s3.first=“Sally”; strncpy(s3.last, “Suzuki”, MAX); s3.age = 33; s3.gpa = 3.33; printStudent(s3);
  31. Example Code  See example program at: typedef.c  Several

    examples of typedef with variables, pointers, structures, and functions % cp typedef.c program.c % make –f makefile-program
  32. Example Code  See example program at: cards.c  Simulates

    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
  33. Memory Management  Structure Initialization  Operator sizeof()  Structures

    and Functions  Structures and Arrays  Using typedef