Hello,
I just read the tutorial provided on this website and the section about pointers and arrays puzzled me a little. Given the information of the tutorial and looking at
1 2 3
char * x;
char y[10] = {'a', 'b'};
x = y;
I expect y beeing a (constant) pointer to the location of the first value of the array. However, sizeof(y) now delivers 10 and not 4 (on a 32-bit system). Thus y has the size of the entire memory which is required for the array's ten values, indicating that y in fact is not a pointer but the array data. Also, printing y to the console gets me "ab" instead of the memory address of the first value in the array. (Btw: The same happens if I print x to the console what does not make sence to me either. Should both not result in the output of the same memory address similar to the case when x references to any other value but an array?)
Afterwards I figured that
1 2
char (*z)[10];
z = &y;
would make more sence to me and that x = y; should not even be a valid assignment since y is apparantly not a pointer but represents stored data.
Beeing quite new both to C++ and pointers I now wonder where my thinking is wrong. Can someone explain to me how this adds up?
Thank you a lot for your help! Rafael
PS: I am referring to "Pointers and Arrays" on http://www.cplusplus.com/doc/tutorial/pointers/
It works, but does it mean that float is an int?
The same way, array and pointer are different things, but when you need a pointer to where your array starts, you can use the array's name as if it was one.
In console you're seeing "ab", because you're printing a char* which is the type used for strings. It's only natural to see a string. If you want the address, cast it to a void* (or any other).
An array is not the same as a pointer, even though you can apply square brackets to dereference both arrays and pointers, making them seem similar.
However, an array will be promoted to a pointer where appropriate. So, if you pass an array to a function, the array will be converted into a pointer to the first element. Same if you assign an array to a pointer.