Advertisement
alansam

Another sequence point error

Apr 3rd, 2020
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.  
  5.   int b[] = { 33,44,55,66,77,88, };
  6.   int *a = b;
  7.  
  8.   for (int i = 0; i < 5; ++i) {
  9.     printf("value: %d, address: %p\n", *a, a++);
  10.   }
  11.  
  12.   return 0;
  13. }
  14.  
  15. $ gcc-9 -Wall -Werror -o sqerror1 sqerror1.c
  16. sqerror1.c: In function 'main':
  17. sqerror1.c:10:45: error: operation on 'a' may be undefined [-Werror=sequence-point]
  18.    10 |     printf("value: %d, address: %p\n", *a, a++);
  19.       |                                            ~^~
  20. cc1: all warnings being treated as errors
  21. $ clang -Wall -Werror -o sqerror1 sqerror1.c
  22. sqerror1.c:10:45: error: unsequenced modification and access to 'a' [-Werror,-Wunsequenced]
  23.     printf("value: %d, address: %p\n", *a, a++);
  24.                                         ~   ^
  25. 1 error generated.
  26.  
  27. # and compiling without -Werror to allow compilation to succeed
  28.  
  29. $ clang -Wall -o sqerror1 sqerror1.c
  30. sqerror1.c:10:45: warning: unsequenced modification and access to 'a' [-Wunsequenced]
  31.     printf("value: %d, address: %p\n", *a, a++);
  32.                                         ~   ^
  33. 1 warning generated.
  34. $ ./sqerror1
  35. value: 33, address: 0x7ffeedaa9480
  36. value: 44, address: 0x7ffeedaa9484
  37. value: 55, address: 0x7ffeedaa9488
  38. value: 66, address: 0x7ffeedaa948c
  39. value: 77, address: 0x7ffeedaa9490
  40. $ gcc-9 -Wall -o sqerror1 sqerror1.c
  41. sqerror1.c: In function 'main':
  42. sqerror1.c:10:45: warning: operation on 'a' may be undefined [-Wsequence-point]
  43.    10 |     printf("value: %d, address: %p\n", *a, a++);
  44.       |                                            ~^~
  45. $ ./sqerror1
  46. value: 44, address: 0x7ffeeb3e3470
  47. value: 55, address: 0x7ffeeb3e3474
  48. value: 66, address: 0x7ffeeb3e3478
  49. value: 77, address: 0x7ffeeb3e347c
  50. value: 88, address: 0x7ffeeb3e3480
  51. Kielder:tmp alansampson$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement