Search code examples
cdereference

Why no dereference operator in char string?


Let's say I have 2 functions:

void function1(int *ptr) {
     printf("%d", *ptr);
}

and

void function2(char *str) {
     printf("%s", str);
}

Why does function2 work, when there is no deference operator before str? In str there is only the address where it points to not the value as I thought.


Solution

  • Why does function2 work, when there is no deference operator before str

    Because %s, as defined by the standard, expects a char *, i.e. an address.

    7.21.6.1

    If no l length modifier is present, the argument shall be a pointer to the initial element of an array of character type. Characters from the array are written up to (but not including) the terminating null character. If the precision is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character.