Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Example output:
- ********* Pointers:
- x == 1 (x's value)
- &x == 0x7ffc778eac50 (x's address)
- p == 0x7ffc778eac50 (p's value == x's address)
- &p == 0x7ffc778eac60 (p's address)
- You can perform arithmetic on pointers
- 0x7ffc778eac50 + 1 == 0x7ffc778eac54
- p + 1 increased the address by 4... why?
- size of integer: 4 bytes
- 0x7ffc778eac50 + 2 == 0x7ffc778eac58
- It works in unit lengths of the base type!
- Can also subtract: 0x7ffc778eac50 - 1 == 0x7ffc778eac4c
- ********* Arrays:
- array byte length: 12
- array element length: 4
- array element count: 3
- array base address: 0x7ffc778eac80
- element values:
- array[0] == 34
- array[1] == 69
- array[2] == 1337
- element addresses:
- array[0] @ 0x7ffc778eac80
- array[1] @ 0x7ffc778eac84
- array[2] @ 0x7ffc778eac88
- Array elements are contiguous (next to each other in memory)
- Array indexing is just pointer arithmetic
- &array[2] == 0x7ffc778eac88
- &2[array] == 0x7ffc778eac88
- &array[0] + 2 == 0x7ffc778eac88
- array + 2 == 0x7ffc778eac88
- All different ways of expressing the same thing.
- C array indexing is "Syntactic sugar" on top of pointer arithmetic
- array[x] will add x to the base address, and then dereference
- Now let's assign "int* p" to alias the zeroth element of the array
- p + 2 == 0x7ffc778eac88
- *(p + 2) == 1337
- */
- #include <stdio.h>
- #include <assert.h>
- int main()
- {
- puts("\n********* Pointers:\n");
- // an "int" represents memory which stores an integer
- int x = 1; // initialize with integer literal 1
- // an "int*" represents memory which stores the address of an "int"
- int* p = &x; // initialized with the address of x
- printf(" x == %i (x's value)\n", x);
- printf("&x == %p (x's address)\n", (void*)&x);
- printf(" p == %p (p's value == x's address)\n", (void*)p);
- printf("&p == %p (p's address)\n\n", (void*)&p);
- puts("You can perform arithmetic on pointers");
- printf("%p + 1 == %p\n", (void*)p, (void*)(p+1));
- puts("p + 1 increased the address by 4... why?");
- printf("size of integer: %lu bytes\n", sizeof(int));
- printf("%p + 2 == %p\n", (void*)p, (void*)(p+2));
- puts("It works in unit lengths of the base type!");
- printf("Can also subtract: %p - 1 == %p\n", (void*)p, (void*)(p-1));
- puts("\n********* Arrays:\n");
- int array[3] = { 0 };
- 0[array] = 34;
- 1[array] = 69;
- 2[array] = 1337;
- /*
- Oh, I bet that looks weird doesn't it :D
- Keep reading to find out wtf is going on!
- Let's print some information about our array
- */
- typedef unsigned long (size_t); // type alias for convenience
- const size_t byte_len = sizeof(array);
- const size_t element_len = sizeof(*array); // effectively "sizeof(int)"
- const size_t array_len = byte_len / element_len;
- printf("array byte length: %lu\n", byte_len);
- printf("array element length: %lu\n", element_len);
- printf("array element count: %lu\n", array_len);
- printf("array base address: %p\n", (void*)array);
- printf(" element values:\n");
- for (unsigned int i = 0; i < array_len; ++i)
- printf("\tarray[%u] == %i\n", i, array[i]);
- printf("\n element addresses:\n");
- for (unsigned int i = 0; i < array_len; ++i)
- printf("\tarray[%u] @ %p\n", i, (void*)&array[i]);
- puts("\nArray elements are contiguous (next to each other in memory)");
- puts("Array indexing is just pointer arithmetic\n");
- printf("&array[2] \t== %p\n", (void*)&array[2]);
- printf("&2[array] \t== %p\n", (void*)&2[array]);
- printf("&array[0] + 2 \t== %p\n", (void*)(&array[0] + 2));
- printf("array + 2 \t== %p\n", (void*)(array + 2));
- puts("\nAll different ways of expressing the same thing.");
- puts("C array indexing is \"Syntactic sugar\" on top of pointer arithmetic");
- puts("array[x] will add x to the base address, and then dereference\n");
- puts("Now let's assign \"int* p\" to alias the zeroth element of the array");
- p = &array[0];
- printf("p + 2 \t\t== %p\n", (void*)(p + 2));
- printf("*(p + 2) \t== %i\n", *(p + 2));
- return 0;
- }
Add Comment
Please, Sign In to add comment