44 Intermediate C Programming
After executing the first line, an integer called a has been created and its value is 632.
The second line creates another integer variable called c and its value is not defined yet.
This is the snapshot of the call stack after finishing line 2.
Symbol Address Value
c 101 garbage
a 100 632
The third line makes c’s value the same as a’s value.
Symbol Address Value
c 101 632
a 100 632
The fourth and the fifth lines create two pointers. Their values are currently undefined.
Symbol Address Value
iptr2 103 garbage
iptr1 102 garbage
c 101 632
a 100 632
The sixth line assigns a’s address to iptr1’s value.
Symbol Address Value
iptr2 103 garbage
iptr1 102 100
c 101 632
a 100 632
The seventh line assigns iptr1’s value, 100, to iptr2’s value.
Symbol Address Value
iptr2 103 100
iptr1 102 100
c 101 632
a 100 632
The third and the seventh lines are similar: The third line assigns a’s value to c’s value;
the seventh line assigns the value of iptr1 to the value of iptr2.
A second way to use pointers is to retrieve the value stored at their addresses.
int a = 632;1
int * iptr ;2
int c;3
iptr = & a;4
c = * iptr ;5
/* read iptr s value as an address , go to that address ,6
read the value at that address , assign the value to c */7
printf (" %d" , * iptr );8
* iptr = -84;9
Pointers 45
Shown below is the call stack after the program finishes the fourth line.
Symbol Address Value
iptr 102 100
c 101 garbage
a 100 632
The fifth line does the following things:
1. Takes iptr’s value as an address. The value is 100.
2. Goes to that address (100).
3. Reads the value at that address and it is 632.
4. Assigns 632 to c.
After the fifth line, the call stack becomes:
Symbol Address Value
iptr 102 100
c 101 632
a 100 632
This is the rule: If a program has something like the following:
= * iptr ; // iptr is a pointer1
the program will
1. take iptr’s value as an address.
2. go to that address.
3. read the value at that address.
4. assign the value to the variable at the left.
This rule is applicable if * iptr is at the right hand side (RHS) of the assignment sign (=).
This is also called dereferencing a pointer. The assignment sign = is not strictly necessary,
in which case the rule still works without part 4 of the assignment. For example, the eighth
line of the code above prints 632.
The emphasis on the “right hand side” is important. When * iptr is on the left hand
side (LHS), it works in a similar but opposite way. For example, the last line of code above
does the following:
1. Takes iptr’s value as an address and it is 100.
2. Goes to that address (100).
3. Modifies the value at address 100 to 84.
Thus, after the last line, the call stack becomes
Symbol Address Value
iptr 102 100
c 101 632
a 100 84
Many students find pointers confusing at first. This confusion is well justified. The same
symbol * has different meanings. The symbol also means multiplication when it is between
two numeric values (integer, float, double). The following table summarizes the different
meanings:
It is time to test your understanding of the different usages of *. Draw the call stack for
the following code snippet:
46 Intermediate C Programming
Example Meaning
1. int * iptr; Create a pointer variable. ptr’s value is an address. An integer is
stored at that address.
* is after the type (int in this case)
2. iptr = & val Assign val’s address to ptr’s value.
This is how to assign a valid address to ptr; note that * is not
used.
3. = * ptr (right hand side of assignment, RHS) Take ptr’s value as an address
and read the value at that address.
= is not always necessary, for example, when printing or calling a
function.
4. * ptr = (left hand side of assignment, LHS) Take ptr’s value as an address
and modify the value at that address.
5. 5 * 17 Multiplication: 5 * 17 is 85.
In this case, * is between two numbers
TABLE 4.1: Different usages of * in C programs. Please notice that ptr = and * ptr =
have different meanings.
int a = 21;1
int c = -4;2
int * ptr ;3
ptr = & a;4
* ptr = 7;5
c = * ptr ;6
* ptr = a * c ;7
After executing the first three lines, the call stack is shown below.
Symbol Address Value
ptr 102 garbage
c 101 4
a 100 21
The fourth line assigns a’s address to ptr’s value.
Symbol Address Value
ptr 102 100
c 101 4
a 100 21
The fifth line has * ptr at the left hand side of the assignment sign. This assigns value
7 to the address 100.
Symbol Address Value
ptr 102 100
c 101 4
a 100 21 7
The sixth line reads the value at address 100; the value is 7. This value is assigned to c.
The call stack is shown below.
Pointers 47
Symbol Address Value
ptr 102 100
c 101 4 7
a 100 7
The seventh line reads the values of a and c; both are 7. The symbol * is used twice.
At the right hand side, * means multiplication and the result is 49. Then, 49 is assigned to
the value at address 100. This changes a’s value to 49.
Symbol Address Value
ptr 102 100
c 101 7
a 100 7 49
4.4 The Swap Function Revisited
Section 4.2 explains that
void swap ( i n t x , i n t y )1
{2
int z = x;3
x = y;4
y = z;5
}6
does not work because the changes to x and y are lost after the swap function finishes (i.e.,
returns) and the top frame is popped. How do you write a correct swap function? The
swap function needs to change the values of a and c. Their addresses reside outside of the
function. To do so, swap must have the addresses of a and c.
void swap ( /* the addresses of a and c */ )1
{2
3
4
5
}6
7
void f( void )8
{9
int a = 83;10
int c = -74;11
swap ( /* the ad dresses of a and c */ );12
/* RL */13
}14
Since the function f must provide the addresses of a and c, the swap function’s arguments
must be pointers that store these addresses.
void swap ( i n t * k , i nt * m )1
{2
48 Intermediate C Programming
3
4
5
}6
7
void f( void )8
{9
int a = 83;10
int c = -74;11
swap (& a , & c);12
/* RL */13
}14
This is the call stack when starting the swap function.
Frame Symbol Address Value
swap
m 104 101
k 103 100
Return Location 102 line 13
f
c 101 74
a 100 83
The following code implements the swap function:1
void swap ( i nt * k , i nt * m )2
{3
int s = * k;4
* k = * m;5
* m = s ;6
}7
8
void f( void )9
{10
int a = 83;11
int c = -74;12
swap (& a, & c ) ;13
}14
The third line reads the value at the address of 100 and stores the value in s.
Frame Symbol Address Value
swap
s 105 83
m 104 101
k 103 100
Return Location 102 line 13
f
c 101 74
a 100 83
The fourth line reads the value stored at address 101; the value is 74. This value is
stored at the address 100.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset