I have a question to the following code:
#include <stdio.h>
int main() {
int i;
char char_array[5] = "abcde";
int int_array[5] = {1, 2, 3, 4, 5};
char *char_pointer;
int *int_pointer;
for(i=0; i < 5; i++) {
printf("[integer pointer] points to %p, which contains the integer %d\n", int_pointer, *int_pointer);
int_pointer = int_pointer + 1;
}
for(i=0; i < 5; i++) {
printf("[char pointer] points to %p, which contains the char '%c'\n", char_pointer, *char_pointer);
char_pointer = char_pointer + 1;
}
}
I didn´t initialized any pointer, just set the pointer´s type. These two pointers are pointing to the right memory address of the array and starts at position [0].
Using Ubuntu 7.04 with gcc compiler.
If I run the code i get these results:
[integer pointer] points to 0xbffff7f0, which contains the integer 1
[integer pointer] points to 0xbffff7f4, which contains the integer 2
[integer pointer] points to 0xbffff7f8, which contains the integer 3
[integer pointer] points to 0xbffff7fc, which contains the integer 4
[integer pointer] points to 0xbffff800, which contains the integer 5
[char pointer] points to 0xbffff810, which contains the char 'a'
[char pointer] points to 0xbffff811, which contains the char 'b'
[char pointer] points to 0xbffff812, which contains the char 'c'
[char pointer] points to 0xbffff813, which contains the char 'd'
[char pointer] points to 0xbffff814, which contains the char 'e'
I know it is not the correct programming style and you should not let the pointers uninitialized, nonetheless I came across this and want to ask you, why does the pointer know to which address he has to point without initializing.
Also I put two more arrays into the code int_array2 and char_array2, first after int_array/char_array and second before int_array/char_array. But I still get the same results...
Maybe someone knows the magic behind this. Thanks in advance for taking your time to answer my question.