Week 8: Structure types and structures in memory Flashcards
What is a structure in essence?
A construct to group together dissimilar
variables under one name
What is the syntax for making a structure?
struct person {
char first[32]; /* 1st field is array of char /
char last[32]; / 2nd field is array of char /
int year; / 3rd field is int /
double ppg; / 4th field is double */
};
If a structure is created whereas the first variable in the structure is called “first” how can you access the memory location of a structure that has been called “ex”?
ex
ex.first
&ex.first[0]
What function can be used to add a string to a character array that has already been created?
strcpy();
ex:
strcpy(teacher.first,”Sam”);
Can you create an array of structures?
YES
ex:
struct fraction { int wP; int fP; }; struct fraction f[3], *g;
With the following code in mind, how what is happening?
struct fraction { int wP; int fP; }; struct fraction f[3], *g;
g = &(f[0]);
g++;
(*g).wP = 5;
g->fP = 11;
Creating an array of 3 fraction structures indexed at 0, 1, and 2.
Creating a pointer to type fraction called g and assigning it the memory location of the first element of the array f.
Increment g by the size of what is points to (fraction structure) which is 2 x the size of an int.
Go to the memory location now stored in g and make the wP variable of that array structure element assigned to 5
Go to the memory location now stored in g and make the fP variable of that array structure element assigned to 11
What are the two ways of accessing a variable inside a structure?
(*structure).variable
or
structure->variable
How do you declare the structure while also making a variable type of the structure in the same line?
struct fraction {
int wP;
int fP;
} fract;
What is the difference between the following:
struct fraction {
int wP;
int fP;
} fract;
typedef struct fraction {
int wP;
int fP;
} fract;
The first creates a structure type and then creates a variable called fract that is of type fraction
The second creates an alias for struct fraction called fract that can be used in place of “struct fraction” when creating variables, etc.
What is the difference between a UNION and a structure?
A struct is a block of memory that stores several data objects, where those objects don’t overlap. A union is a block of memory that stores several data objects, but has only storage for the largest of these, and thus can only store one of the data objects at any one time.
Union sets aside how much space in memory?
Enough space to store the largest of the data types in the union
The address of any of the different variables in a union all return ___
The same address
What does realloc do EXACTLY
Is used to resize allocated memory without losing existing data. However, it will only increase or decrease to the given size. The given size is not the size to be added or subtracted, it is the size that will be the final size.
Ex:
double *a; a = (double *)malloc(5 *sizeof(double)); a = (double *) realloc (a, 9*sizeof(double)); 9 x 8 bytes increase to 9 double variables 72 byes // could have used a = (double *) realloc (a, 3*sizeof(double)); - decrease to 3 double variables 24 bytes.