Lecture 5: Memory Structure and Pointers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structures Revisted
struct date {
int month, day, year;
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structures containing structures:
struct period {
struct date start;
struct date end;
};
struct period {
struct {int month; int day; int year} start, end;
};
Initialization:
struct period holiday = {{5,2,1997}, {8,26,1997}};
int start_day = holiday.start.day;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Arrays and Structures:
struct employee {
int ssn; /* social security number */
int hours; /* hours per week */
float pay; /* amount they are paid */
};
#define EMPLOYEE_NUM 3
struct employee payroll[EMPLOYEE_NUM] =
{{444523234, 40, 200.00},
{513123124, 50, 250.00},
{523554635, 40, 220.00}}
void print_payroll(struct employee payroll[], int employee_num)
{
int i;
for (i=0;i < employee_num;i++){
printf("SSN: %d; Hours per week: %d; Pay: %f\n",
payroll[i].ssn, payroll[i].hours, payroll[i].pay);
}
}
int main(void)
{
print_payroll(payroll, EMPLOYEE_NUM);
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global (External) Variables and Local Variables
- Although you didn't notice, I just defined a global variable,
something I haven't done before
- Global variables(payroll) last for the entire program
- Local variables(i) only last for the particular function call
Another example:
int x = 17;
void foo(int y)
{
int z = 3;
printf("%d\n", x + y + z);
return;
}
void bar(void)
{
int i = 3;
foo(17);
print("%d\n", x + i);
return;
}
int main (void)
{
int j = -2;
bar ();
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Functions Revisited
int c = 3, d = 4;
/* Wrong Swap */
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
return;
}
int main(void)
{
int a = 1, b = 2;
swap(a,b);
printf("%d, %d\n",a,b);
swap(c,d);
printf("%d, %d\n",c,d);
return 0;
}
- the values of the arguments are *copied*. These *copies* are used by
the C functions. Any assignments to these variables do not affect the
arguments after the function returns.
- consider:
swap(3,4);
- we can't write a swap function that works with what we know now!!!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Memory Structure: What is really happening.
- computer memory is really just a big array of "slots" (each slot has
and index or address like an array)
- memory is divided into two areas(really 3 as we will see later):
- the static area
- the stack
- static area: the place where variables which last the entire length of
the program are stored. Global variables. (x from the foo program)
- stack: place for function parameters and local variables
- run through the swap example - show the updates to the stack copies
which don't affect the values in actual arguments.
- we want to be able to write the swap function!
- we want to be able to change x and y inside a function.
- instead of passing the values of a and b, we want to pass a *reference*
to a and a *reference* to b. We can do this by passing their
*addresses* - a *pointer* to a or b
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return;
}
int c = 3, d = 4;
int main(void)
{
int a = 1, b = 2;
swap(& a, & b);
printf("%d, %d\n", a, b);
swap(& c, & d);
printf("%d, %d\n", c, d);
return 0;
}
- "&" is the "take the address of" operator
- "*" is the "get the thing pointed to" operator