#include <stdio.h>
int main()
{
int i;
char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
int int_array[5] = {1, 2, 3, 4, 5};
unsigned int hacky_nonpointer;
hacky_nonpointer = (unsigned int) char_array;
for(i=0; i<5; i++)
{ // Iterate through hacky_nonpointer with the sizeof(char).
printf("[hacky_nonpointer] points to %p, which contains the char '%c'\n",
hacky_nonpointer, *((char *) hacky_nonpointer));
hacky_nonpointer = hacky_nonpointer + sizeof(char);
}
// char_pointer = (void *) ((char *) char_pointer + 1);
// ==> inc by char* data_typ then conv back to void *
hacky_nonpointer = (unsigned int) int_array;
for(i=0; i<5; i++)
{ // Iterate through hacky_nonpointer with the size_of(int) func.
printf("[hacky_nonpointer] points to %p, which contains the integer %d\n",
hacky_nonpointer, *((int *) hacky_nonpointer));
hacky_nonpointer = hacky_nonpointer + sizeof(int);
}
}
root@Brien:~# gcc -g -o ./pointer_types5 pointer_types5.c
pointer_types5.c: In function ‘main’:
pointer_types5.c:12:21: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pointer_types5.c:17:24: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
pointer_types5.c:24:21: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pointer_types5.c:29:24: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
root@Brien:~# ./pointer_types5
Segmentation fault
root@Brien:~#