2

I am just getting used to pointers in C. I was thinking of using %s to print the first character of a character array.

In the following code, why does %s not work, but %c works? If *my_string is dereferencing the first value of the character array, wouldn't %s try to print all the characters in memory starting at that address and continue until a null-zero character is reached?

Also, how is printf("%s", my_string) working, but printf("%s" *my_string) is not?

char* my_string = "The original value.";
printf("Value of my_string: %s\n", my_string);
my_string = "The new value.";
printf("Value of my_string: %s\n", *my_string); // Error
printf("Value of my_string: %c\n", *my_string); // Prints only the first letter 'T', but does not have any errors
2
  • 1
    %s expects a null terminated string, *my_string is not a pointer, it is a char value, and %s tries to use that value as a pointer, which is obviously wrong. Commented Aug 20, 2020 at 6:13
  • " If *my_string is dereferencing the first value" It is. Meaning it goes to the address and fetch the contents there. "wouldn't %s try to print all the characters in memory starting at that address" But my_string is the address, *my_string is the contents of that address. %s expects the former, not the latter. Commented Aug 20, 2020 at 10:24

3 Answers 3

1

The asterisk * de-references the pointer to the type that it is pointing to, so when my_string is a pointer to a character, *my_string is a character. Your C compiler sees your format string containing a %s and a parameter of *my_string which is a character and shows you an error.

To output the whole string, print it as in your first call to printf, to print only a single character do as you did in your last call to printf.

Sign up to request clarification or add additional context in comments.

Comments

1

why does %s not work, but %c works?

It is the rule by which how the printf function works. For "%s" it expects a pointer to a null ending string, and for "%c" it expects a single character.

If *my_string is dereferencing the first value of the character array, wouldn't %s try to print all the characters in memory starting at that address and continue until a null-zero character is reached?

Yes. So you must be careful in case it is a string too long and these characters are printable before the null ending.

Also, how is printf("%s", my_string) working, but printf("%s" *my_string) is not?

For "%s", it expects a pointer. "*my_string" is a character.

1 Comment

Good first answer.
1

To understand this we need to go into how the string "The original value." is stored in the memory and what the char* my_string points to.

We can expect a memory structure like this

0xDEADBEEF    ASCII T
0xDEADBEEF+1  ASCII h
0xDEADBEEF+2  ASCII e
0xDEADBEEF+3  ASCII <space>
...
0xDEADBEEF+16 ASCII u
0xDEADBEEF+17 ASCII e
0xDEADBEEF+18 ASCII .
0xDEADBEEF+19       0

So when you have a char* my_string it points to the first character or in this case the address 0xDEADBEEF. So when you use %s in a printf it will loop through all the characters starting from the first one you pointed to until it reaches \0. But *my_string just reads the first character on that address. For that reason we need to give it a pointer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.