Pointers 59
4.9 Exercises
4.9.1 Swap Function 1
Does this program have any syntax problems because of wrong types (such as assigning
an integer to a pointer’s value)? Will this function actually swap the values of u and t?
What is the program’s output? Please draw the call stack and explain.
// swap1 .c1
#in clude < stdio .h >2
#in clude < stdlib .h >3
void swap1 ( i n t a , in t b )4
{5
int k = a;6
a = b;7
b = k;8
}9
int main ( i n t argc ,char * * argv )10
{11
int u;12
int t;13
u = 17;14
t = -96;15
printf (" before swap1 : u = %d , t = %d n " , u , t );16
swap1 ( u , t);17
printf (" after swap1 : u = %d , t = %d n " , u , t );18
return EXIT _SUCCES S ;19
}20
4.9.2 Swap Function 2
How about this program?
// swap2 .c1
#in clude < stdio .h >2
#in clude < stdlib .h >3
void swap2 ( i n t * a , i n t * b)4
{5
int * k = a;6
a = b;7
b = k;8
}9
10
int main ( i n t argc ,char * * argv )11
{12
int u;13
int t;14
u = 17;15
t = -96;16
printf (" before swap2 : u = %d , t = %d n " , u , t );17
60 Intermediate C Programming
swap2 (& u , & t);18
printf (" after swap2 : u = %d , t = %d n " , u , t );19
return EXIT _SUCCES S ;20
}21
4.9.3 Swap Function 3
How about this program?
// swap3 .c1
#in clude < stdio .h >2
#in clude < stdlib .h >3
4
void swap3 ( i n t * a , i n t * b)5
{6
int k = * a;7
a = b;8
* b = k ;9
}10
11
12
int main ( i n t argc ,char * * argv )13
{14
int u;15
int t;16
u = 17;17
t = -96;18
printf (" before swap3 : u = %d , t = %d n " , u , t );19
swap3 (& u , & t);20
printf (" after swap3 : u = %d , t = %d n " , u , t );21
return EXIT _SUCCES S ;22
}23
4.9.4 Swap Function 4
How about this program?
// swap4 .c1
#in clude < stdio .h >2
#in clude < stdlib .h >3
4
void swap4 ( i n t * a , i n t * b)5
{6
int k = * a;7
* a = * b;8
* b = * k;9
}10
11
12
int main ( i n t argc ,char * * argv )13
{14
Pointers 61
int u;15
int t;16
u = 17;17
t = -96;18
printf (" before swap4 : u = %d , t = %d n " , u , t );19
swap4 (& u , & t);20
printf (" after swap4 : u = %d , t = %d n " , u , t );21
return EXIT _SUCCES S ;22
}23
4.9.5 Swap Function 5
How about this program?
// swap5 .c1
#in clude < stdio .h >2
#in clude < stdlib .h >3
4
void swap5 ( i n t * a , i n t * b)5
{6
int k = a;7
a = b;8
b = k;9
}10
11
int main ( i n t argc ,char * * argv )12
{13
int u;14
int t;15
u = 17;16
t = -96;17
printf (" before swap5 : u = %d , t = %d n " , u , t );18
swap5 (& u , * t);19
printf (" after swap5 : u = %d , t = %d n " , u , t );20
return EXIT _SUCCES S ;21
}22
4.9.6 15,552 Variations
There are many variations of the swap function. To be specific, there are 15,552 variations
and only one of them is correct. Some variations have syntax errors (wrong types) and some
of them do not swap the values in the main function. Let me explain why there are so many
variations. First, this is the correct swap function and the correct way to call it:
void swap ( i nt * k , i nt * m )1
{2
int s;3
s = * k ;4
* k = * m;5
* m = s ;6
}7
62 Intermediate C Programming
8
void f( void )9
{10
int a = 83;11
int c = -74;12
swap (& a, & c ) ;13
}14
How do we get 15,552 variations? In the first line, there are two options for k:
1. int k
2. int * k
int & k is illegal so it is not considered.
Similarly, there are two options for m and two options for s. So far, there are 8 variations
of the function up to the third line. Next, consider the number of options for s = k at the
fourth line; there are six options:
1. s = * k;
2. s = & k;
3. s = k;
4. * s = * k;
5. * s = & k;
6. * s = k;
& s = is illegal so it is not considered.
Similarly, there are also six options for k = m and another six options for m = s. So far
there are 8 × 6 × 6 × 6 = 1,728 variations for swap function.
From the main function, calling swap has three options for using a in the thirteenth line:
1. a
2. & a
3. * a
Similarly, there are another three options in using c. Thus, in total, there are 1,728 × 3
× 3 = 15,552 variations.
Among all these variations, if the swap function is called without using addresses, the
changes are lost when the swap function finishes. In other words, regardless what happens
inside swap, calling swap in this way,
swap (a , c) ;1
is always wrong.
4.10 Answers
4.10.1 Swap Function 1
There are no syntax errors or warnings but this function does not swap u and k. This is
the output of the program:
before swap1: u = 17 , t = -96
after swap1: u = 17 , t = -96
Pointers 63
4.10.2 Swap Function 2
There are no syntax errors or warnings but this function does not swap u and k. This is
the call stack after finishing line 7, before swap2 finishes. The values of a and b are swapped
but the values of u and t remain unchanged.
Frame Symbol Address Value
swap
k 105 100
b 104 100
a 103 101
Return Location 102 line 18
main
t 101 96
u 100 17
This is the output of the program:
before swap1: u = 17 , t = -96
after swap1: u = 17 , t = -96
4.10.3 Swap Function 3
There are no syntax errors or warnings. The problem is the seventh line. This line assigns
b’s value to a’s value. This is the call stack after finishing the seventh line:
Frame Symbol Address Value
swap
k 105 17
b 104 101
a 103 101
Return Location 102 line 18
main
t 101 96
u 100 17
This is the output of the program. Both u and t are 17, and the value 96 has been
discarded.
before swap3: u = 17 , t = -96
after swap3: u = 17 , t = 17
4.10.4 Swap Function 4
The eighth line has a problem: k is an integer and adding * in front of k is invalid. This
program will not compile.
4.10.5 Swap Function 5
The sixth line has a problem: k is an integer but a is a pointer. It is invalid to assign
a pointer’s value to an integer. The eighteenth line also has a problem: t is an integer and
adding * in front of t is invalid.
..................Content has been hidden....................

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