Home > OS >  Why does the pointer get printed twice in C?
Why does the pointer get printed twice in C?

Time:12-26

If I compile the following code and run it with gcc source.c && ./a.out:

#include <stdio.h>

int main(void) {
    char string[] = "My string";
    char m = string[0];
    printf("%s\n", &m);
}

I get MMy string as the output, but if I do

#include <stdio.h>

int main(void) {
    char string[] = "My string";
    char m = string[0];
    printf("%s\n", &string[0]);
}

I get just My string as expected. What is going on here? I noticed that if I define the pointer as a variable, e.g.

#include <stdio.h>

int main(void) {
    char string[] = "My string";
    char *m = &string[0];
    printf("%s\n", m);
}

Then again I get My string as expected. Why does the value of the pointer get printed twice in the first example?

CodePudding user response:

The first program has undefined behavior.

The expression &m does not point to a character of a string. It points to a single object of the type char

char string[] = "My string";
char m = string[0];

After the object m there can be anything in the memory. It just occurred such a way that after the object m there is the array string without any gap.

But the conversion specifier %s expects a pointer to a string that is to a sequence of characters that is terminated by the zero character '\0'.

In the second program the pointer m points to the first character of the string stored in the array string.

char string[] = "My string";
char *m = &string[0];

So the second program is correct.

  • Related