Table of Contents for
Intermediate C Programming

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Intermediate C Programming by Yung-Hsiang Lu Published by CRC Press, 2015
  1. Front Cover
  2. Contents (1/2)
  3. Contents (2/2)
  4. List of Figures (1/2)
  5. List of Figures (2/2)
  6. List of Tables
  7. Foreword
  8. Preface
  9. Author, Reviewers, and Artist
  10. Rules in Software Development
  11. Source Code
  12. I. Computer Storage: Memory and File
  13. 1. Program Execution (1/2)
  14. 1. Program Execution (2/2)
  15. 2. Stack Memory (1/5)
  16. 2. Stack Memory (2/5)
  17. 2. Stack Memory (3/5)
  18. 2. Stack Memory (4/5)
  19. 2. Stack Memory (5/5)
  20. 3. Prevent, Detect, and Remove Bugs (1/2)
  21. 3. Prevent, Detect, and Remove Bugs (2/2)
  22. 4. Pointers (1/6)
  23. 4. Pointers (2/6)
  24. 4. Pointers (3/6)
  25. 4. Pointers (4/6)
  26. 4. Pointers (5/6)
  27. 4. Pointers (6/6)
  28. 5. Writing and Testing Programs (1/4)
  29. 5. Writing and Testing Programs (2/4)
  30. 5. Writing and Testing Programs (3/4)
  31. 5. Writing and Testing Programs (4/4)
  32. 6. Strings (1/3)
  33. 6. Strings (2/3)
  34. 6. Strings (3/3)
  35. 7. Programming Problems and Debugging (1/4)
  36. 7. Programming Problems and Debugging (2/4)
  37. 7. Programming Problems and Debugging (3/4)
  38. 7. Programming Problems and Debugging (4/4)
  39. 8. Heap Memory (1/3)
  40. 8. Heap Memory (2/3)
  41. 8. Heap Memory (3/3)
  42. 9. Programming Problems Using Heap Memory (1/4)
  43. 9. Programming Problems Using Heap Memory (2/4)
  44. 9. Programming Problems Using Heap Memory (3/4)
  45. 9. Programming Problems Using Heap Memory (4/4)
  46. 10. Reading and Writing Files (1/3)
  47. 10. Reading and Writing Files (2/3)
  48. 10. Reading and Writing Files (3/3)
  49. 11. Programming Problems Using File (1/2)
  50. 11. Programming Problems Using File (2/2)
  51. II. Recursion
  52. 12. Recursion (1/4)
  53. 12. Recursion (2/4)
  54. 12. Recursion (3/4)
  55. 12. Recursion (4/4)
  56. 13. Recursive C Functions (1/4)
  57. 13. Recursive C Functions (2/4)
  58. 13. Recursive C Functions (3/4)
  59. 13. Recursive C Functions (4/4)
  60. 14. Integer Partition (1/5)
  61. 14. Integer Partition (2/5)
  62. 14. Integer Partition (3/5)
  63. 14. Integer Partition (4/5)
  64. 14. Integer Partition (5/5)
  65. 15. Programming Problems Using Recursion (1/5)
  66. 15. Programming Problems Using Recursion (2/5)
  67. 15. Programming Problems Using Recursion (3/5)
  68. 15. Programming Problems Using Recursion (4/5)
  69. 15. Programming Problems Using Recursion (5/5)
  70. III. Structure
  71. 16. Programmer-Defined Data Types (1/6)
  72. 16. Programmer-Defined Data Types (2/6)
  73. 16. Programmer-Defined Data Types (3/6)
  74. 16. Programmer-Defined Data Types (4/6)
  75. 16. Programmer-Defined Data Types (5/6)
  76. 16. Programmer-Defined Data Types (6/6)
  77. 17. Programming Problems Using Structure (1/4)
  78. 17. Programming Problems Using Structure (2/4)
  79. 17. Programming Problems Using Structure (3/4)
  80. 17. Programming Problems Using Structure (4/4)
  81. 18. Linked Lists (1/3)
  82. 18. Linked Lists (2/3)
  83. 18. Linked Lists (3/3)
  84. 19. Programming Problems Using Linked List (1/2)
  85. 19. Programming Problems Using Linked List (2/2)
  86. 20. Binary Search Trees (1/4)
  87. 20. Binary Search Trees (2/4)
  88. 20. Binary Search Trees (3/4)
  89. 20. Binary Search Trees (4/4)
  90. 21. Parallel Programming Using Threads (1/5)
  91. 21. Parallel Programming Using Threads (2/5)
  92. 21. Parallel Programming Using Threads (3/5)
  93. 21. Parallel Programming Using Threads (4/5)
  94. 21. Parallel Programming Using Threads (5/5)
  95. IV. Applications
  96. 22. Finding the Exit of a Maze (1/5)
  97. 22. Finding the Exit of a Maze (2/5)
  98. 22. Finding the Exit of a Maze (3/5)
  99. 22. Finding the Exit of a Maze (4/5)
  100. 22. Finding the Exit of a Maze (5/5)
  101. 23. Image Processing (1/3)
  102. 23. Image Processing (2/3)
  103. 23. Image Processing (3/3)
  104. 24. Huffman Compression (1/10)
  105. 24. Huffman Compression (2/10)
  106. 24. Huffman Compression (3/10)
  107. 24. Huffman Compression (4/10)
  108. 24. Huffman Compression (5/10)
  109. 24. Huffman Compression (6/10)
  110. 24. Huffman Compression (7/10)
  111. 24. Huffman Compression (8/10)
  112. 24. Huffman Compression (9/10)
  113. 24. Huffman Compression (10/10)
  114. A. Linux
  115. B. Version Control
  116. C. Integrated Development Environments (IDE) (1/3)
  117. C. Integrated Development Environments (IDE) (2/3)
  118. C. Integrated Development Environments (IDE) (3/3)
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.