On another forum, I went to help a user with a question to determine how many elements a char pointer array had.
I managed to solve it (by sheer luck I think), and as I have no training in software, I could not understand the value returned by the function.
The code I'm posting is pretty simple.
I used the function : sizeof().
My difficulties to understand:
Because when using this function with the square brackets "[ ]", and any index, it always returns the value 2, and when I use it without the square brackets it returns twice the value of the number of elements.
Thank you very much in advance for your time and for your reply.
An array of 6 pointer occupies 12 bytes (on an 8-bit AVR).
A pointer to a char occupies 2 bytes (on an 8-bit AVR).
12/2 = 6 ---> An array with 6 element has 6 elements.
BTW, sizeof is not strictly a "function" as its value is computed at compile time.
Arrays in C/C++ is just a pointers to row of variables of the same type.
color is an array and, simultaneously - the pointer to its first (zero indexed) element.
color[0] is an array element and since color is an array of pointers - color[0] is itself a pointer - it point to string "blue"
Strictly speaking you are right, even arrays with same type elements, but different length considered as different data types, for example when used as template arguments.
But from the point of view of pointer arithmetic, the name of an array is just a pointer to its first element.
So I think that this statement is too strong:
But I agree that I wrote inaccurately and should not mislead beginners