Radoslaw Krasimirow
Radoslaw Krasimirow

Reputation: 1873

How is the string literal argument interpreted when passed as parameter in function?

I need help to grasp something. Currently I am testing the code on microcontroller platform with small amount of memory (ROM(FLASH), RAM). This is the code

void print(const char * c)
{
   printf("%#4x, ",c);
   printf("%s\n",c);
}

This is the function call

print("Riko");

And the output is: 0x5C4C, Riko

The memory address 0x5C4C resides in FLASH (MAIN CODE MEMORY) so the literal string "Riko" must also reside in that memory segment? My question is: When we pass "Riko" as argument to the function print, does it actually mean that we pass the address of the first character of the string "Riko" or ((const char*) 0x5C4C) as argument to the function print? Thanks a lot...

Upvotes: 0

Views: 126

Answers (2)

autistic
autistic

Reputation: 15642

Yes. According to section 6.3.2.1, paragraph 3 of the C standard...

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.

Upvotes: 0

Giorgi Moniava
Giorgi Moniava

Reputation: 28654

When we pass "Riko" as argument to the function print, does it actually mean that we pass the address of the first character of the string "Riko"

Yes, it means that, however, for printing address you should use:

   printf("%p", (void*)c);

Upvotes: 4

Related Questions