Guest User

Untitled

a guest
Nov 16th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. // all the same:
  2. // *p++ = x
  3. // *(p)++ = x
  4. // *(p++) = x
  5. //
  6. #include <stdio.h>
  7.  
  8. void print_arr(int a[])
  9. {
  10. for(int i=0; i<3; i++) {
  11. printf("%d\n", a[i]);
  12. }
  13. }
  14.  
  15. void set_arr(int *p, int v1, int v2, int v3)
  16. {
  17. *p++ = v1;
  18. *p++ = v2;
  19. *p++ = v3;
  20. }
  21.  
  22. void set_arr2(int *p, int v1, int v2, int v3)
  23. {
  24. *(p)++ = v1;
  25. *(p)++ = v2;
  26. *(p)++ = v3;
  27. }
  28.  
  29. void set_arr3(int *p, int v1, int v2, int v3)
  30. {
  31. *(p++) = v1;
  32. *(p++) = v2;
  33. *(p++) = v3;
  34. }
  35.  
  36. #if 0
  37. void set_arr4(int *p, int v1, int v2, int v3)
  38. {
  39. (*p)++ = v1; // error: expression is not assignable
  40. (*p)++ = v2; // error: expression is not assignable
  41. (*p)++ = v3; // error: expression is not assignable
  42. }
  43. #endif
  44.  
  45. int main(void)
  46. {
  47. int arr[3];
  48. int *parr = arr;
  49.  
  50. set_arr(parr, 3, 2, 1);
  51. print_arr(arr);
  52. printf("---------\n");
  53.  
  54. set_arr2(parr, 6, 5, 4);
  55. print_arr(arr);
  56. printf("---------\n");
  57.  
  58. set_arr3(parr, 9, 8, 7);
  59. print_arr(arr);
  60.  
  61. #if 0 // error occured
  62. set_arr4(parr, 111, 444, 777);
  63. print_arr(arr);
  64. #endif
  65.  
  66. return 0;
  67. }
  68.  
  69. // result
  70. // ----------------------------------
  71. // ~/test/c ❯❯❯ gcc ptr_plus_plus.c && ./a.out
  72. // 3
  73. // 2
  74. // 1
  75. // ---------
  76. // 6
  77. // 5
  78. // 4
  79. // ---------
  80. // 9
  81. // 8
  82. // 7
  83. // ~/test/c ❯❯❯
Add Comment
Please, Sign In to add comment