[\<- Notes 01/13](01-13.md) --- Notes 01/15/2020 # Review Pointers Below is an example to demonstrate pointer precedence: ``` int x[] = {0, 5, 10, 15, 20, 25, ...}; int y = 5; int *p = x; y = ++*p printf("%d, %d\n", y, *p); y = *(++p); printf(...); //ditto y = ++(*p); printf(...); //ditto ``` The code outputs the following: ``` 1, 1 5, 5 6, 6 ``` --- # Introduction to Structures in C - Earlier we talked about data types: int, char - A Structure allows for multiple data types in one array - Structure Basics - A structure is a collection of values, called members - Outline of Structure Definition: ``` struct name{ variable declaration; variable declaration; ... }; ``` - The keyword struct signifies that this is a structure definition - Example: ``` struct info{ int number; char character; char string[10]; int array[10]; }; ``` - As can be seen in the example, multiple different data types are included as members of `info` - This example **does not allocate any memory** - Instead, it simply tells C that now there is a new kind of data type called `struct info` - Let's create a sample `struct info`: ``` struct info real_info; struct info *p; ``` - Memory **is** allocated for real\_info - Member variables of a struct can be accessed using `.` - For example: ``` p = &real_info //*p will point to real_info now real_info.number = 2; real_info.character = 'A'; ``` - Let's see a visual representation of real\_info in memory | real_info.number | real_info.character | real_info.string[] | real_info.array[] | | - | - | - | - | | 2 | 'A' | ... | ... | - `real_info.string[]` and `real_info.array[]` show `...` because to show the full visual representation in memory, I would have to show `real_info.string[0]`, `real_info.string[1]`, etc. - To access structure members with a pointer (such as `*p` defined above), use `->` - For example: ``` p->number = 2; p->character = 'B'; p->array[0] = 3; ``` - The above code changes three members of real\_info in memory to the following: | real_info.number | real_info.character | real_info.string[] | real_info.array[0] | | - | - | - | - | | 2 | 'B' | ... | 3 | --- - Another Example: ``` struct node{ int number; struct node *link; }; ``` - We will talk more about links later --- [-> Notes 01/17](01-17.md)