Reputation: 309
I know this question has been answered several times but am not able to make sense of the following script. I am new to C by the way.
As I get it, if an array used as a value it represents the address of first character aka pointer.
if I run this:
int main (){
char quote[] = "C is great"
printf ("The quote: %s\n",quote);
printf ("The address of quote is %p\n",quote);
printf ("Size of quote is %lu\n",sizeof(quote));
}
I get:
The quote: C is great
The address of quote is 0x7fff06fa0d90
Size of quote is 11
So my question is in all printf cases, I have used used the same variable quote, but by changing print type how does it change from value to pointer and where is the pointer representation stored because sizeof gives me length of the string.
Thanks!
Upvotes: 4
Views: 896
Reputation: 106012
Array type conversion to pointer type has some exceptions.
Except when it is the operand of the
sizeof
operator, the_Alignof
operator, or the unary&
operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue.
Note that the statement
printf ("The address of quote is %p\n",quote);
prints the address of first element of quote
, not the address of quote
although you will get the same address value on printing the address of quote
.
Note that _Alingof
is mentioned in draft is an error.
Upvotes: 1
Reputation: 300
All variables are pointers to memory locations in C. There is nothing else. The compiler keeps track of the variables by their pointers. It also keeps track of the variable type and size but these are not strict. Every variable is guaranteed to have a pointer, but type and consequently size can be changed.
The type and size is what determines what value is represented when a variable is used in your program but as far as your program is concerned, it actually has no idea what is stored at each pointer, except that the compiler has specifically written it into your compiled program.
You can use casting to override the type (even read memory out of bounds if you are not careful). If you have an array of 12 chars, you can read the values as an array of 3 ints if you want, for example.
In the case of static arrays (like your example), either when you define the size of the array or when you assign the value to the array, the compiler allocates the memory it needs.
It knows the length either because you told it explicitly in your definition (char quote[11]
) or because it knows what has been stored in the array ("C is great"
= 10). Since a char type variable is 1 byte, then 10 of them must be 10 bytes.
Your array is actually 11 bytes. This is because of how and what you assigned to it. In C, when the compiler finds a string literal, it automatically appends a NULL
char on the end. If it didn't do this, functions like printf
wouldn't know how long the string is. It wouldn't know because the size and type are not stored with the variable.
If you wrote your array as:
char quote[] = {'C',' ','i','s',' ','g','r','e','a','t'};
The compiler wouldn't know that it's a string and your array would be 10 bytes instead of 11. It might also behave strangely when you try to printf
as a string in this case, since printf
relies on the NULL
byte to know when it reached the end of the string.
printf ("The quote: %s\n",quote);
Upvotes: -1
Reputation: 67203
Internally, a pointer is an integer value that holds an address. So in all cases you are passing this pointer value to printf()
.
But the format specifier determines what printf()
will do with that pointer. In one case, it reads the characters that are at the address contained in the pointer. In another case, it simply prints the pointer value.
Upvotes: 0
Reputation: 141586
The pointer representation is not stored in a designated location. It has the same "storage" as the result of 2 + 2
. (Usually, this will be a register). These are called values officially in C; sometimes called rvalues for discussion purposes.
The conversion happens for both of the cases where you give quote
as argument to printf
. The %s
or the %p
tells printf whether to output a representation of the pointer received, or whether to follow that pointer and print out the characters at the other end.
The rvalue is formed whenever it is needed. The sizeof
operator does not perform lvalue-to-rvalue conversion on its operand, so sizeof(quote)
does not generate or do any operation on a pointer.
Upvotes: 3