Lecture 7: Structures Flashcards
A good location to leave structures is…
at the top of your files (or in header files).
Defining a structure
tells the compiler about the new type, but doesn’t allocate storage.
Declaring a variable
tells the compiler the type and identifier (name) you chose.
What happens when you pass a structure to a function?
It is passed by value.
Ways to assign values to struct
struct example name = { a, b, c }; // Bad practice. Don’t.
struct example name = { .x = a, .y = b, .z = c } // Much better.
struct example name = { .x = a }; // y and z uninitialized.
Compound literals.
name = (struct example) { .x = a, .y = b, .z = c };
This is used to assign a value to something already declared. Or, you can just set piece by piece.
Arrays of structs
struct example[] = { {.x = a}, {.x = b} };
struct example[] = { [0].x = a, [1].x=b }
Can structs contain other structs?
Yes.
Can you define a structure without a name?
You could. But it’s not recommended.
I.e.
struct
{
// stuff goes here
} arr_of_these[n];
go back to slides and find information about memory. the stack. etc
What operations are allowed for the stack?
“push” and “pop”. Add an element to the top, or remove it.
What happens to memory when a function is called?
An entry is pushed onto the stack, containing arguments, space for variables in the function, and a return address.
Stack grows when a function is called and shrinks as it returns. It grows from wherever it is.
Note: in gdb, bt will show the contents of the stack. Alternatively, bt –full
notes on processes