Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3.  
  4. #define SIZE 10
  5.  
  6. void fun_with_pointers_and_arrays(); // defined below.
  7. void fun_with_strings();
  8. void the_most_fun();
  9.  
  10. int main()
  11. {
  12. int x;
  13.  
  14. // This is a declaration of the variable p of type
  15. // "int *", which means it is a pointer to an int.
  16. // A pointer is simply an address.
  17. int *p;
  18.  
  19. x = 6;
  20.  
  21. printf("x is initially %d\n", x);
  22.  
  23. // The & operator is the "address of" operator, so that &x
  24. // returns the address x. In this case, the address of
  25. // x is being written into p, so that p points to x.
  26.  
  27. p = &x; // p now points to x
  28.  
  29. // The * operator is the dereferencing operator, which simply
  30. // indicates that a pointer should be followed. That is,
  31. // *p refers not to p itself, but to whatever p points
  32. // to -- in this case, p points to x.
  33.  
  34. *p = 7; // This actually modifies x to be 7.
  35.  
  36. printf("p points to %d\n", *p);
  37. printf("Now x = %d\n", x);
  38.  
  39. fun_with_pointers_and_arrays(); // see the function definition below
  40.  
  41. fun_with_strings(); // see below
  42.  
  43. the_most_fun(); // see below
  44.  
  45. }
  46.  
  47. void fun_with_pointers_and_arrays()
  48. {
  49.  
  50. printf("\nIn fun_with_pointers_and_arrays()\n");
  51.  
  52. // When you declare an array, such as the array "arr" below,
  53. // and specify its size, then space in memory is allocated
  54. // for the array. The name of the array, in this case arr,
  55. // is just a constant pointer to the first element of the array.
  56.  
  57. int arr[SIZE];
  58.  
  59. // Notice that, in this case, arr and &arr[0] (the address of the
  60. // first element of a, are the same thing -- they're both addresses.
  61.  
  62. printf("arr = %p, &arr[0] = %p\n", arr, &arr[0]);
  63.  
  64. // Now we're declaring an int pointer q and setting it to
  65. // point to the first element of the array (which arr already
  66. // points to).
  67.  
  68. int *q = arr;
  69.  
  70. // Here is a loop that shows that as we increment q (using q++) to point
  71. // to subsequent elements of arr, q is being incremented by 4 each time,
  72. // since q is an int pointer and int's are 4 bytes. That is, if q
  73. // points to an int, then in order for q to point to the next int, the
  74. // address stored in q to be incremented by 4. IMPORTANT: The compiler
  75. // takes care of adding 4 (rather than 1) to q when q++ is executed. The
  76. // programmer doesn't need to worry about it.
  77.  
  78. for(int i=0; i< SIZE; i++) {
  79. printf("q = %p\n", q); // printing q in hex each time to show the increment by 4
  80. *q = i*2; // writing to where q points to, which is an element of arr.
  81. q++; // incrementing q to point to the next element of arr.
  82. }
  83.  
  84. // Using ordinary array notation, arr[i], we print out the
  85. // elements of arr.
  86.  
  87. for(int i=0; i< SIZE; i++) {
  88. printf("%d ", arr[i]);
  89. }
  90. printf("\n");
  91.  
  92. }
  93.  
  94.  
  95. void fun_with_strings()
  96. {
  97.  
  98. printf("\nIn fun_with_strings()\n");
  99.  
  100. // There is no separate "string" type in C. A string is simply an
  101. // array of char's (bytes). In order to treat such an array as
  102. // a string, you need to write a 0 (not the character '0') at
  103. // the end of the string. This way, the printf function, and lots
  104. // of other functions that you will use to operate on strings,
  105. // knows where the end of the string is (at the 0).
  106.  
  107. // declaring a variable as an array of char's, which we will
  108. // treat below as a string by making sure there's a 0 at the
  109. // end.
  110.  
  111. char s[80];
  112.  
  113. // declaring a char pointer r and initializing it to point to
  114. // the start of the array s.
  115.  
  116. char *r = s;
  117.  
  118. // Declaring a char variable c and initializing it to the character 'a'.
  119. // Remember that a char is just a one-byte number, so in this case 'a'
  120. // is the ASCII value representing the lower-case character 'a'.
  121. // If you look that up in the ACII table, you'll see the value is
  122. // 97 in decimal and 61 in hexadecimal.
  123.  
  124. char c = 'a';
  125.  
  126. printf("The character %c is %d decimal and %x hex in the ASCII table.\n",
  127. c, c, c);
  128.  
  129. // This loop shows three things: (1) The use of the pointer r to iterate
  130. // over the elements of the char array s, (2) Since r is a char pointer,
  131. // and a char is one byte, incrementing r only increases it by 1, and
  132. // (3) since c is a one-byte number (containing an ASCII code), you can
  133. // increment it to refer to the next character in the ASCII table.
  134.  
  135. for(int i = 0; i < 26; i++) {
  136. printf("r = %p\n", r); // Printing the address stored in r
  137. *r = c; // Writing into s via the pointer r
  138. r++; // Incrementing r, increasing it by 1.
  139. c++; // Incrementing c so it contains the ASCII value for the
  140. // next character.
  141. }
  142.  
  143. *r = 0; // REMEMBER to write a 0 into the end of the array, so the
  144. // printf function will know when to stop printing.
  145.  
  146. // In printf, the %s format specifier will cause a char array to be
  147. // printed as a string. That is, it will print the character corresponding
  148. // to the ASCII code in each element of the array and will stop when
  149. // it hits a 0.
  150.  
  151. printf("s = %s\n", s);
  152.  
  153. }
  154.  
  155.  
  156. // This function shows some fun C code that uses pointers.
  157.  
  158. void the_most_fun()
  159. {
  160. printf("\nIn the_most_fun()\n");
  161. // (I didn't discuss this in class yet)
  162. // If you just want a variable to point to a string, an easy way
  163. // to do this is by putting the string in double quotes. The compiler will
  164. // generate code that allocates sufficient space for the specified string,
  165. // including a 0 at the end.
  166.  
  167. // Here, s1 is a char pointer that points to an array containing the ASCII
  168. // values for the characters 'H', 'e', 'l', 'l', 'o', ' ' (space), 'W', 'o'
  169. // 'r', 'l', 'd'. The last element of the array is 0 (not the character '0').
  170.  
  171. char *s1 = "Hello World";
  172.  
  173. // Declaring an character array s2.
  174.  
  175. char s2[80];
  176.  
  177. // p1 is a pointer to the first element of the array (string) that s1 points to.
  178.  
  179. char *p1 = s1;
  180.  
  181. // p2 is a pointer to the first element of the array s2.
  182.  
  183. char *p2 = s2;
  184.  
  185. // The code below copies the characters in s1 into s2, but in a very
  186. // concise and difficult-to-read way. YOU SHOULD NOT WRITE CODE LIKE THIS.
  187. // It relies on the fact that (1) the value of an assignment
  188. // operation, =, is whatever value is assigned and (2) C treats
  189. // 0 as false, so that when the 0 is copied from *p1 into *p2,
  190. // the value of assignment expression is 0, so that the while
  191. // loop stops.
  192. // Note: the way to read the expression "*p2++ = *p1++" is "take the
  193. // value that p1 points to and write it into the place that p2 points
  194. // to, then increment p1 and increment p2."
  195.  
  196. while ((*p2++ = *p1++)); // the extra parenthesis is to suppress a compiler warning.
  197.  
  198. // Sure enough, "Hello World" has been copied into s2:
  199.  
  200. printf("s2 = %s\n", s2);
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement