Week 8: Structure types and structures in memory Flashcards

1
Q

What is a structure in essence?

A

A construct to group together dissimilar

variables under one name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the syntax for making a structure?

A

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 */
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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”?

A

ex

ex.first

&ex.first[0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What function can be used to add a string to a character array that has already been created?

A

strcpy();

ex:
strcpy(teacher.first,”Sam”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you create an array of structures?

A

YES

ex:

struct fraction {
int wP;
int fP;
};
struct fraction f[3], *g;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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;

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the two ways of accessing a variable inside a structure?

A

(*structure).variable

or

structure->variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you declare the structure while also making a variable type of the structure in the same line?

A

struct fraction {
int wP;
int fP;
} fract;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between the following:

struct fraction {
int wP;
int fP;
} fract;

typedef struct fraction {
int wP;
int fP;
} fract;

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between a UNION and a structure?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Union sets aside how much space in memory?

A

Enough space to store the largest of the data types in the union

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The address of any of the different variables in a union all return ___

A

The same address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does realloc do EXACTLY

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly