1

Let's say I have the following string:

char *my_string = "Stack";

As far as I know char * holds the memory address of the first character of the string "Stack". In the computer memory it might be represented as the following:

 ------------------------
| S | t | a | c | k | \0|
 ------------------------
  ^
  my_string

If I call: printf("%s\n", my_string);, the entire string is printed. How does the compiler know to print the entire string? Since as I understand, it only has an address of a character.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
jkigel
  • 1,592
  • 6
  • 28
  • 49
  • 5
    the `%s` says: print all chars till you find a `'\0'` – mch Oct 10 '14 at 08:05
  • http://stackoverflow.com/questions/6799470/can-a-pointer-to-a-string-be-used-in-a-printf – Gopi Oct 10 '14 at 08:09
  • The compiler doesn't print the string, `printf` does. It does it by printing each character one at a time, until it hits the NUL. It's hard to see what your difficulty with this is. – Jim Balter Oct 10 '14 at 08:50
  • 1
    The same way your address identifies your whole house, not just the front door. – luser droog Oct 10 '14 at 08:55

6 Answers6

8

The char* indeed points only to the first character of your string, however functions like printf("%s") will simply start reading and continue until they find a 0-byte. String literals like your "Stack" example are zero-terminated by default, thus printf will know to print your string and stop after that.

MrHug
  • 1,315
  • 10
  • 27
  • 1
    Indeed @MrHug, Great explanation, just wanted to add something that explains this with low level languages. 6 bytes are used to store "Stack" (1 byte for each letter plus a NULL byte at the end). The compiler will put this in the Data segment (reference to assembly section). This memory (location and values) is set at compile time and cannot be modified at run time (if you try it will segfault). – PradyumanDixit Oct 31 '18 at 12:26
6

The compiler doesn't do anything other than compile your program.

The library function printf, however, makes the following requirement to you (and you happen to meet this requirement): When given a format specifier %s, the corresponding argument must be of type char *, and is assumed to point to the first element of a zero-terminated array of characters. The function will then print out consecutive elements of this array until it hits a zero byte.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2

Since it knows how large a character is, it can compute the address of the 2nd character and print that (by adding 1 to the original pointer, basically). Then it can do the same thing again to get to the third character, and so on until a character with the value '\0' is found and the string is considered to end.

unwind
  • 391,730
  • 64
  • 469
  • 606
2

char* is just a pointer that points to the beginning of the string. Many C functions (printf, strcpy, strlen, ...) depend on the terminating '\0' at the end of the string into which a passed to them pointer happens to point to. Always remember to terminate string with '\0' when passing pointer to string to such functions to avoid undefined behavior, segmentation fault, access violation, etc.

4pie0
  • 29,204
  • 9
  • 82
  • 118
1

Compiler dont know anything about the size or type of the variable's address passed , so when you pass the first address of the char , and use specifier of string %s , Only thing compiler knows is the address passed and that it had to and interpret the address as a string .So as usually printf prints all the characters and end the execution on reading \0 null pointer. And Strings are basically just collection of char ended by a null pointer \0.

0

char *my_string = "Stack"; This implies the following things:

  1. my_string is a pointer to a char 'S'
  2. On incrementing the pointer it will be increased by sizeof(char).

As most have cited what it does, adding to it an observation: If you overwrite the '\0' address of a normal string with anything else, it will continue to print until it encounters '\0' (a technique to induce cyber attacks).

thepace
  • 2,221
  • 1
  • 13
  • 21