Skip to main content
Code-ified the NUL in the description as `0`
Source Link
John Burger
  • 1.9k
  • 1
  • 13
  • 23

That's where a C convention for strings steps in. By convention, when the compiler sees a string "between quotes", it stores the ASCII values of the characters, and then follows them with a 00. Not an ASCII '0' (with value 48), but an ASCII NUL (with value 0). Thus if you looked at address 0x3004 (the byte directly after John), you'd see a 0 in that memory byte. That's how printf() knows to stop printing - and yes, many a bug has resulted from forgetting to maintain a NUL at the end of a sequence of chars!

That's where a C convention for strings steps in. By convention, when the compiler sees a string "between quotes", it stores the ASCII values of the characters, and then follows them with a 0. Not an ASCII '0' (with value 48), but an ASCII NUL (with value 0). Thus if you looked at address 0x3004 (the byte directly after John), you'd see a 0 in that memory byte. That's how printf() knows to stop printing - and yes, many a bug has resulted from forgetting to maintain a NUL at the end of a sequence of chars!

That's where a C convention for strings steps in. By convention, when the compiler sees a string "between quotes", it stores the ASCII values of the characters, and then follows them with a 0. Not an ASCII '0' (with value 48), but an ASCII NUL (with value 0). Thus if you looked at address 0x3004 (the byte directly after John), you'd see a 0 in that memory byte. That's how printf() knows to stop printing - and yes, many a bug has resulted from forgetting to maintain a NUL at the end of a sequence of chars!

added 304 characters in body
Source Link
John Burger
  • 1.9k
  • 1
  • 13
  • 23
  • printf("%c", *s); wouldn’t compile.

    s is not a pointer, so you can’t dereference it with *.

  • printf("%c", *p_s); would print the character at where p_s was pointing.

    This would print out J.

  • printf("%c", s[0]); would print the character at ss[0].

    This would print out J.

  • printf("%c", s); would give horrible results.

    Again, you lied to printf()!

  • printf("%s", s); would work (but see below).

    %s means "I've got a sequence of characters that I'd like you to print. I've provided the address of the first character."

  • printf("%s", p_s); would be identical to the previous one.

    After all, the same address value was passed!

  • printf("%s", *p_s); would give horrible results.

    Again, you lied to printf()!

which would have reserved 1010 bytes and only initialised the first 5 of them. The following would be an error:

char *p_b = (char *)malloc(size); // Where size is a variable
 
char *buffer = new char[size];    // Grab 'size' new chars
free(p_b);
 
delete [] buffer;
  • printf("%c", *s); would print the character at s.

    This would print out J.

  • printf("%c", s); would give horrible results.

    Again, you lied to printf()!

  • printf("%s", s); would work (but see below).

    %s means "I've got a sequence of characters that I'd like you to print. I've provided the address of the first character."

  • printf("%s", p_s); would be identical to the previous one.

    After all, the same address value was passed!

which would have reserved 10 bytes and only initialised the first 5 of them. The following would be an error:

char *p_b = (char *)malloc(size); // Where size is a variable
 
char *buffer = new char[size];    // Grab 'size' new chars
free(p_b);
 
delete [] buffer;
  • printf("%c", *s); wouldn’t compile.

    s is not a pointer, so you can’t dereference it with *.

  • printf("%c", *p_s); would print the character at where p_s was pointing.

    This would print out J.

  • printf("%c", s[0]); would print the character at s[0].

    This would print out J.

  • printf("%c", s); would give horrible results.

    Again, you lied to printf()!

  • printf("%s", s); would work (but see below).

    %s means "I've got a sequence of characters that I'd like you to print. I've provided the address of the first character."

  • printf("%s", p_s); would be identical to the previous one.

    After all, the same address value was passed!

  • printf("%s", *p_s); would give horrible results.

    Again, you lied to printf()!

which would have reserved 10 bytes and only initialised the first 5 of them. The following would be an error:

char *p_b = (char *)malloc(size); // Where size is a variable
char *buffer = new char[size];    // Grab 'size' new chars
free(p_b);
delete [] buffer;
Added an alternative to the `s` option
Source Link
John Burger
  • 1.9k
  • 1
  • 13
  • 23

which would have reserved 1010 bytes and only initialised the first 5 of them. The following would be an error:

char *p_b = (char *)malloc(size); // Where size is a variable 

char *buffer = new char[size];    // Grab 'size' new chars
free(p_b); 

delete [] buffer;

which would have reserved 10 bytes and only initialised the first 5 of them. The following would be an error:

char *p_b = (char *)malloc(size); // Where size is a variable
char *buffer = new char[size];    // Grab 'size' new chars
free(p_b);
delete [] buffer;

which would have reserved 10 bytes and only initialised the first 5 of them. The following would be an error:

char *p_b = (char *)malloc(size); // Where size is a variable 

char *buffer = new char[size];    // Grab 'size' new chars
free(p_b); 

delete [] buffer;
Subtle improvements to readability
Source Link
John Burger
  • 1.9k
  • 1
  • 13
  • 23
Loading
Closed a parenthesis and a quote
Source Link
John Burger
  • 1.9k
  • 1
  • 13
  • 23
Loading
Punctuation
Source Link
John Burger
  • 1.9k
  • 1
  • 13
  • 23
Loading
Source Link
John Burger
  • 1.9k
  • 1
  • 13
  • 23
Loading