#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_nonpointe r;
hacky_nonpointe r = (unsigned int) char_array;
for(i=0; i<5; i++)
{ // Iterate through hacky_nonpointe r with the sizeof(char).
printf("[hacky_nonpointe r] points to %p, which contains the char '%c'\n",
hacky_nonpointe r, *((char *) hacky_nonpointe r));
hacky_nonpointe r = hacky_nonpointe r + sizeof(char);
}
// char_pointer = (void *) ((char *) char_pointer + 1);
// ==> inc by char* data_typ then conv back to void *
hacky_nonpointe r = (unsigned int) int_array;
for(i=0; i<5; i++)
{ // Iterate through hacky_nonpointe r with the size_of(int) func.
printf("[hacky_nonpointe r] points to %p, which contains the integer %d\n",
hacky_nonpointe r, *((int *) hacky_nonpointe r));
hacky_nonpointe r = hacky_nonpointe r + 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:~#
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_nonpointe r;
hacky_nonpointe r = (unsigned int) char_array;
for(i=0; i<5; i++)
{ // Iterate through hacky_nonpointe r with the sizeof(char).
printf("[hacky_nonpointe r] points to %p, which contains the char '%c'\n",
hacky_nonpointe r, *((char *) hacky_nonpointe r));
hacky_nonpointe r = hacky_nonpointe r + sizeof(char);
}
// char_pointer = (void *) ((char *) char_pointer + 1);
// ==> inc by char* data_typ then conv back to void *
hacky_nonpointe r = (unsigned int) int_array;
for(i=0; i<5; i++)
{ // Iterate through hacky_nonpointe r with the size_of(int) func.
printf("[hacky_nonpointe r] points to %p, which contains the integer %d\n",
hacky_nonpointe r, *((int *) hacky_nonpointe r));
hacky_nonpointe r = hacky_nonpointe r + 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:~#
Comment