I have a question about strings and pointers. I'm currently watching Harvard's cs50 course and I'm a little confused about strings.
The lecturer explains that strings are just pointers to the first memory address. I believe I understand that, but my question was just in the printf function when using "%s" is this kind of like a function call to run a loop and find where the string ends? since we're only storing the first memory address that's the only way I could picture that working in my head
//strings are just addresses of the first character in char
const char *s = "HI!";
//how to print a string
printf("%s", s);
Also I'm only using const
because I'm writing this in a C file
and I'm sure in real life understanding the source code of printf
probably doesn't benefit me much but I think the answer will just help me wrap my head around this concept a bit.
CodePudding user response:
Here are two ways of seeing how a string really works inside. As you know, a string in C is an array of characters, terminated by a "null" or "zero" or \0
character. So we can step over those characters, one at a time, ourselves, looking for the terminating null character. This is similar to what printf
is doing, deep down inside, when you ask it to print a string using %s
.
#include <stdio.h>
int main()
{
const char *string = "hello";
int i;
for(i = 0; i < 10; i ) {
printf("string[%d] = %c\n", i, string[i]);
if(string[i] == '\0') break;
}
const char *p;
for(p = string; *p != '\0'; p )
printf("next char: %c\n", *p);
}
In the first loop, we use the "index" variable i
to step through the cells of the array. We print each character out as we find it. We also check for the \0
character and, when we find it, we break out of the loop, because there will be no more useful characters after that. This part of the code prints
string[0] = h
string[1] = e
string[2] = l
string[3] = l
string[4] = o
string[5] =
You can't "see" the \0
character in string[5]
, because it doesn't print as anything.
This first loop is a little strange the way I wrote it, because the i < 10
condition in for(i = 0; i < 10; i )
doesn't really mean anything. Actually I could have left it out and written for(i = 0; ; i )
, since I have my own termination condition if(string[i] == '\0') break;
to break out of the loop and stop it.
The second loop steps over the string using a pointer variable instead, const char *p
. It prints:
next char: h
next char: e
next char: l
next char: l
next char: o
If you're just starting out in C, you're probably not ready to think about pointers in detail yet. But they're a very important part of C, and they're very closely related to arrays, so they're extremely useful in working with strings.
CodePudding user response:
printf()
, in and of itself, is a very complex function that uses what's called a format string. The format string specifies how the variable argument(s) should be treated and displayed.
When you pass %s
to printf()
as a format string, you're essentially telling it that the variable argument that follows the format string should be treated as a character array (which is assumed to be zero-terminated)
So yes, printf()
, along with many other functions with string printing capabilities are essentially loops that start at the address of the first character and iteratively prints each character until it finds the end of the string, which is indicated by the null character.
CodePudding user response:
Yes, a string in C is just the address of the first character. It is typically null-terminated. For your example,
const char s[] = "HI!";
Here sizeof(s)
is 4 rather than 3 because a zero value (or null in technical terms) comes after '!'.
CodePudding user response:
c appends string literals with a byte 0, so the memory of s is the following bytes: {'H', 'I', '!', '\0'}
printf("%s",s)
writes the characters until it finds char '\0'