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!
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 wherep_s
was pointing.This would print out
J
.printf("%c", s[0]);
would print the character atss[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 ats
.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 wherep_s
was pointing.This would print out
J
.printf("%c", s[0]);
would print the character ats[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;
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;