alaestor

C pointer arithmetic demo

Jun 26th, 2022 (edited)
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1. /** Example output:
  2.  
  3. ********* Pointers:
  4.  
  5.  x == 1 (x's value)
  6. &x == 0x7ffc778eac50 (x's address)
  7.  p == 0x7ffc778eac50 (p's value == x's address)
  8. &p == 0x7ffc778eac60 (p's address)
  9.  
  10. You can perform arithmetic on pointers
  11. 0x7ffc778eac50 + 1 == 0x7ffc778eac54
  12. p + 1 increased the address by 4... why?
  13. size of integer: 4 bytes
  14. 0x7ffc778eac50 + 2 == 0x7ffc778eac58
  15. It works in unit lengths of the base type!
  16. Can also subtract: 0x7ffc778eac50 - 1 == 0x7ffc778eac4c
  17.  
  18. ********* Arrays:
  19.  
  20. array byte length: 12
  21. array element length: 4
  22. array element count: 3
  23. array base address: 0x7ffc778eac80
  24.  element values:
  25.     array[0] == 34
  26.     array[1] == 69
  27.     array[2] == 1337
  28.  
  29.  element addresses:
  30.     array[0] @ 0x7ffc778eac80
  31.     array[1] @ 0x7ffc778eac84
  32.     array[2] @ 0x7ffc778eac88
  33.  
  34. Array elements are contiguous (next to each other in memory)
  35. Array indexing is just pointer arithmetic
  36.  
  37. &array[2]   == 0x7ffc778eac88
  38. &2[array]   == 0x7ffc778eac88
  39. &array[0] + 2   == 0x7ffc778eac88
  40. array + 2   == 0x7ffc778eac88
  41.  
  42. All different ways of expressing the same thing.
  43. C array indexing is "Syntactic sugar" on top of pointer arithmetic
  44. array[x] will add x to the base address, and then dereference
  45.  
  46. Now let's assign "int* p" to alias the zeroth element of the array
  47. p + 2       == 0x7ffc778eac88
  48. *(p + 2)    == 1337
  49. */
  50.  
  51. #include <stdio.h>
  52. #include <assert.h>
  53.  
  54. int main()
  55. {
  56.  
  57.     puts("\n********* Pointers:\n");
  58.  
  59.     // an "int" represents memory which stores an integer
  60.     int x = 1; // initialize with integer literal 1
  61.    
  62.     // an "int*" represents memory which stores the address of an "int"
  63.     int* p = &x; // initialized with the address of x
  64.  
  65.     printf(" x == %i (x's value)\n", x);
  66.     printf("&x == %p (x's address)\n", (void*)&x);
  67.     printf(" p == %p (p's value == x's address)\n", (void*)p);
  68.     printf("&p == %p (p's address)\n\n", (void*)&p);
  69.  
  70.     puts("You can perform arithmetic on pointers");
  71.     printf("%p + 1 == %p\n", (void*)p, (void*)(p+1));
  72.     puts("p + 1 increased the address by 4... why?");
  73.     printf("size of integer: %lu bytes\n", sizeof(int));
  74.     printf("%p + 2 == %p\n", (void*)p, (void*)(p+2));
  75.     puts("It works in unit lengths of the base type!");
  76.     printf("Can also subtract: %p - 1 == %p\n", (void*)p, (void*)(p-1));
  77.  
  78.     puts("\n********* Arrays:\n");
  79.  
  80.     int array[3] = { 0 };
  81.     0[array] = 34;
  82.     1[array] = 69;
  83.     2[array] = 1337;
  84.     /*
  85.         Oh, I bet that looks weird doesn't it :D
  86.         Keep reading to find out wtf is going on!
  87.         Let's print some information about our array
  88.     */
  89.     typedef unsigned long (size_t); // type alias for convenience
  90.     const size_t byte_len = sizeof(array);
  91.     const size_t element_len = sizeof(*array); // effectively "sizeof(int)"
  92.     const size_t array_len = byte_len / element_len;
  93.     printf("array byte length: %lu\n", byte_len);
  94.     printf("array element length: %lu\n", element_len);
  95.     printf("array element count: %lu\n", array_len);
  96.     printf("array base address: %p\n", (void*)array);
  97.  
  98.     printf(" element values:\n");
  99.     for (unsigned int i = 0; i < array_len; ++i)
  100.         printf("\tarray[%u] == %i\n", i, array[i]);
  101.    
  102.     printf("\n element addresses:\n");
  103.     for (unsigned int i = 0; i < array_len; ++i)
  104.         printf("\tarray[%u] @ %p\n", i, (void*)&array[i]);
  105.  
  106.     puts("\nArray elements are contiguous (next to each other in memory)");
  107.     puts("Array indexing is just pointer arithmetic\n");
  108.     printf("&array[2] \t== %p\n", (void*)&array[2]);
  109.     printf("&2[array] \t== %p\n", (void*)&2[array]);
  110.     printf("&array[0] + 2 \t== %p\n", (void*)(&array[0] + 2));
  111.     printf("array + 2 \t== %p\n", (void*)(array + 2));
  112.  
  113.     puts("\nAll different ways of expressing the same thing.");
  114.     puts("C array indexing is \"Syntactic sugar\" on top of pointer arithmetic");
  115.     puts("array[x] will add x to the base address, and then dereference\n");
  116.    
  117.     puts("Now let's assign \"int* p\" to alias the zeroth element of the array");
  118.     p = &array[0];
  119.     printf("p + 2 \t\t== %p\n", (void*)(p + 2));
  120.     printf("*(p + 2) \t== %i\n", *(p + 2));
  121.  
  122.     return 0;
  123. }
Add Comment
Please, Sign In to add comment