Hi
When I have the following,
then is it correct that str[0] is the pointer to the first char in "hi"?Code:char strarr[][4] = {"hi", "all"};
Best,
Niles.
Hi
When I have the following,
then is it correct that str[0] is the pointer to the first char in "hi"?Code:char strarr[][4] = {"hi", "all"};
Best,
Niles.
No, it is not a pointer.
strarr[0] is an array of 4 characters, "hi\0\0"
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
But using
printf("%s", strarr[0]);
outputs "hi", and the "handle" of a string is the pointer to its first element? I.e., I thought that strarr[0] is also the pointer to the first char.
Where am I wrong in my reasoning?
Another way of looking at it: (*(*strarr)) == str[0][0], hence &(*(*strarr)) is the address of 'h' in "hi\0\0". But &(*(*strarr)) == *strarr, i.e. *strarr is the address of 'h'.
Correct?
You're getting confused by when an array is an array, and when it decays into a pointer to the first element of the array.
Perhaps some examples for illustration
And read this (both of you)Code:#include <stdio.h> #include <stdlib.h> #if 0 arr1 +---+---+---+---+---+---+---+---+---+---+---+---+ | h | i | . | . | . | . | a | l | l | . | . | . | +---+---+---+---+---+---+---+---+---+---+---+---+ arr1 is just 12 bytes of contiguous memory arr2 +------+ +---+---+---+ | |--->| h | i | . | +------+ +---+---+---+---+ | |--->| a | l | l | . | +------+ +---+---+---+---+ arr2 is 3 separate blocks of memory which could be anywhere in memory in relation to one another. There is the array of pointers itself, and the separate storage for each string. #endif int main ( ) { char arr1[][6] = { "hi", "all" }; char *arr2[] = { "hi", "all" }; // sizeof tests { printf("%lu %lu\n", (unsigned long)sizeof(arr1[0]), (unsigned long)sizeof(arr2[0]) ); } // pointer tests { char *p1 = arr1[0]; // arr1[0] is an array, which decays to a pointer to the 1st element char *p2 = arr2[0]; // pointer to start of first string printf("%s %s\n", p1, p2 ); printf("%s %s\n", arr1[0], arr2[0] ); // arr1[0] is a decaying array } // pointer tests // pointing at the whole array returns a DIFFERENT pointer type. // Most specifically, it is NOT a char** as one might assume. { char (*p1)[6] = &arr1[0]; // pointer to an array of 6 chars char **p2 = &arr2[0]; // pointer to a pointer printf("%s %s\n", *p1, *p2 ); } return 0; }
Arrays and Pointers
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
IMHO, all i meant was that the interpretation of strarr[0] depends on the context in which it is used. Using it in a printf(), as the o/p did, means that strarr[0] decays to a pointer to its first element (&strarr[0][0]). For the sizeof operation it is a 4 element array of chars.
Again the interpretation of whether it is a char* or a char[] depends on the context in which it is used.
I must admit I am a little confused of when an array decays to a pointer to the 1st element. In sizeof it does not, in printf it does.
Is there a general rule? And thank you for the detailed example; it concretized my knowledge.
sizeof is an operator - which by the way, only works on arrays declared in its current scope.
printf is a function, not an operator. All arrays passed to functions are passed as a pointer to the first element of the array being passed; i.e., the address of the first element of the array.
Quzah.
Hope is the first step on the road to disappointment.
Niels, note what Quzah referred to in his post:
If you use sizeof(Array), in a function that was passed Array in a parameter, then you will ONLY get the sizeof(some pointer), NOT the sizeof() the array.
Sizeof() only works on local (automatic), arrays when used in their "home" function. That and the non-contiguous memory of pointers (like Salem showed in Array2), always freaks out beginners in C. OK, the memory bit, still makes me scratch my head occasionally.
> Is there a general rule? And thank you for the detailed example; it concretized my knowledge.
Yes, question 6.3 of the FAQ I pointed you to lists them.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Thanks. They say that:
>> (The exceptions are when the array is the operand of a sizeof or & operator, or is a string literal initializer for a character array. [...])
In your example in post #6 you have
You say arr1[0] decays - but wasn't that an exception?Code:char *p1 = arr1[0]; // arr1[0] is an array, which decays to a pointer to the 1st element
> You say arr1[0] decays - but wasn't that an exception?
The & later on is one of the exceptions (&arr1[0])
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
In that case I don't understand what the following exception is:
"The exceptions are when the array is the operand of a sizeof or & operator, or is a string literal initializer for a character array."
Can you please make an example stating this exception? This was it is impossible for me to misunderstand it,
I really appreciate all your help - I am learning a lot from this thread (bookmarked!).