-4

They say a string is naturally an array of characters which represents the memory location of its first character: string == &string[0]. That is why a user do not have to specify ampersand before a string variable when he wants the program to read data into this variable:

scanf("%s", string);

But then there is a reasonable question: why don't we see the string's memory location when printing string out?

printf("%s", string);
Kaiyaha
  • 448
  • 3
  • 9
  • 1
    If it decays to a pointer in one instance why would you expect it not to in both? – Retired Ninja Jul 18 '20 at 08:33
  • Could you be more precise about what exactly you expect it to print? Create a minimal main function and show the input, expected output and actual output. – klutt Jul 18 '20 at 08:44
  • @klutt I expect what is represented by &string[0] – the string memory location. Since string == &string[0], I expect string to give the same value as &string[0], that is what the question was about. – Kaiyaha Jul 18 '20 at 08:47
  • If `p` is a pointer, the expressions `p` and `&p[0]` are the same. – M. Nejat Aydin Jul 18 '20 at 08:50
  • @Kaiyaha Yes, but that's not clear in the question. It's a bit ambigous if you mean that the literal string "&string[0]" should be printed or the address of i t. – klutt Jul 18 '20 at 08:51
  • Did you read the documentation? Your question is like: why I did not get a burger when ordered soup. IMO it is time to read some C book. – 0___________ Jul 18 '20 at 08:53
  • @klutt How can there be a literal string if there is an ampersand before it? – Kaiyaha Jul 18 '20 at 08:53
  • @Kaiyaha No offence, but while I think that it would be very strange to expect the literal string, I think it's equally strange to expect the address to be printed. :) – klutt Jul 18 '20 at 09:00
  • That is why we want people to create a [mre] to get rid of any ambiguity. Examples are usually superior to most explanations. – klutt Jul 18 '20 at 09:01
  • @klutt The question isn't about string literals. OP just uses an array called `string` with a string stored inside. – RobertS supports Monica Cellio Jul 18 '20 at 09:01
  • @Klutt. to be fair, an MCVE would not help here. Showing the expected output would help more – Gerhardh Jul 18 '20 at 09:02
  • @P__J__ I asked for an answer, not for a piece of advice :) – Kaiyaha Jul 18 '20 at 09:04
  • To get rid of any ambiguity, the question was about this: if string == &string[0], why doesn't the outcome contain the string's memory address when we require string to be printed out? The question was dumb since I didn't pay attention to the format specifier, but that's all, hope I cleared it up – Kaiyaha Jul 18 '20 at 09:10
  • @Kaiyaha Still, a post should not depend on it's comments. So you should [edit](https://stackoverflow.com/posts/62966372/edit) your question so that it is clear without the comment section. – klutt Jul 18 '20 at 09:15
  • @klutt done, please check this out – Kaiyaha Jul 18 '20 at 09:17

2 Answers2

4

But then there is a reasonable question: why don't we see &string[0] when printing string out?

printf("%s", string);

I assume you mean that you would expect to see the address printed instead of the content.

This is what the format specifier is for. You pass an address of the first character to printf and because there is a %s format specifier for that parameter, the function knows that it has to take the address, dereference it and print any characters until a 0 is found in memory.

If you had provided a %p format specifier, you would see the address printed. In this case you need to cast the address to void* to be fully standard compliant.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/218083/discussion-on-answer-by-gerhardh-why-does-a-string-not-return-its-memory-locatio). – Samuel Liew Jul 18 '20 at 12:59
2

They say a string is naturally an array of characters which represents the memory location of its first character: string == &string[0]

Two small clarifications here.

A string is technically not an array of characters. It is defined as "a contiguous sequence of code units terminated by the first zero code unit" Or as RobertS said in comments: "A string can be (and high-probably is) stored in an array, but a string is not an array. A string is just a sequence of characters terminated by the null character. A string isn't an array and an array isn't a string."

If string is an array and not a pointer, then they will have different types. string is of type char[] and &string[0] is of type char*. However, if you do the comparison string == &string[0] then string will decay to char* and the expression will evaluate to true.

Related:

Is an array name a pointer?

Can it cause problems to pass the address to an array instead of the array?

klutt
  • 30,332
  • 17
  • 55
  • 95
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/218088/discussion-on-answer-by-klutt-why-does-a-string-not-return-its-memory-location). – Samuel Liew Jul 18 '20 at 12:59