0

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...

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Radoslaw Krasimirow
  • 1,833
  • 2
  • 18
  • 28
  • 2
    You should use `%p` to print pointers (and cast the argument to `void *`). – Kninnug Aug 20 '15 at 08:27
  • @Kninnug Please read [6.2.5p28](http://www.iso-9899.info/n1570.html#6.2.5p28) (and the footnote following it) – autistic Aug 20 '15 at 08:51
  • 1
    @Freenode-newbostonSebivor that does not apply to variadic functions, like `printf`. Please read [this answer](http://stackoverflow.com/a/20435076/249237). – Kninnug Aug 20 '15 at 08:53
  • @Kninnug Actually, the paragraph I quoted (and the footnote following it) *does* apply to this particular scenario, though your citation does not (and isn't authoritarian, if I might add)... The footnote says, in case you can't be bothered reading it, "The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions." Unfortunately, the answer you quoted doesn't consider this... – autistic Aug 20 '15 at 08:56

2 Answers2

4

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);
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • **Note:** While the (mandated for standard compliance) cast to `void*` seems uselessly redundant (and on all modern platforms I know of, *is*), it does have a reason to be: On some platforms, pointers can have a different representation depending on the pointed type, making the cast necessary to ensure the pointer is passed in the representation `printf()` expects. But personnally, on a C99-compliant platform, I'd use an inline function instead to enjoy the implicitness of the cast: `const void* tovoid(const void *p) { return p; }` – Medinoc Aug 20 '15 at 08:47
  • 1
    @Medinoc In this case, it is uselessly redundant, even on ancient platforms `void *` is required to have the same representation and alignment requirements as `char *`. See [6.2.5p28](http://www.iso-9899.info/n1570.html#6.2.5p28) (and the footnote following it). – autistic Aug 20 '15 at 08:52
0

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.

autistic
  • 1
  • 3
  • 35
  • 80