Reputation: 25
Right now I hope to write my own my_strchr()
in the C language.
I checked that the answer should be like this:
char *my_strchr(const char *src1, int c) {
while (*src1 != '\0') { //when the string goes to the last, jump out
if (c == *src1) {
return src1;
}
src1++;
}
return NULL;
}
I'm quite confused by:
why we use *src1
in the while
loop condition (*src1 != '\0')
, is *src1
a pointer to the const char*
? Can we just use src1
instead?
When we return value and src1++
, we do not have that *src1
, why?
For this function, it in fact prints the whole string after the required characters, but why is that? Why does it not print only the character that we need to find?
Upvotes: 1
Views: 774
Reputation: 46
It's important here to remember that in C a string is a series of characters that ends with a null ('\0'
) character. We reference the string in our code using a character pointer that points to the beginning of the string. When we pass a string as a parameter to a function what we're really getting is a pointer to the first character in the string.
Because of this fact, we can use pointer math to increment through a string. The pattern:
while (*src1 != '\0') {
//do stuff
src1++;
}
is a very common idiom in C. We might phrase it in English as:
While the value of the character in the string we are looking at (dereference src1
with the *
operator) is not (inequality operator !=
) the end of string indicator (null byte, 0
or '\0'
), do some stuff, then move the pointer to point to the next character in the string (increment operator ++
).
We often use the same kind of code structure to process other arrays or linked lists of things, comparing pointers to the NULL
pointer.
To question #2, we're returning the value of the pointer from this function src1
and not the value of what it points to *scr1
because the question that this function should answer is "Where is the first instance of character c
in the string that starts at the location pointed to by src1
.
Question #3 implies that the code that calls this function is printing a string that starts from the pointer returned from this function. My guess is that the code looks something like this:
printf("%s", my_strchr(string, 'a'));
printf()
and friends will print from the location provided in the argument list that matches up with the %s
format specifier and then keep printing until it gets to the end of string character ('\0'
, the null terminator).
Upvotes: 1
Reputation: 145297
In addition to Jabberwocky's answer, please note that the code in the question has 2 bugs:
c
must be converted to char
for the comparison with *src1
: strchr("ABC", 'A' + 256)
returns a pointer to the string literal unless char
has more than 8 bits.
Furthermore, if c
converted to a char
has the value 0
, a pointer to the null terminator should be returned, not NULL
as in the posted code.
Here is a modified version:
char *my_strchr(const char *s, int c) {
for (;;) {
if ((char)c == *s) {
return src1;
}
if (*s++ == '\0') {
return NULL;
}
}
}
Upvotes: 0
Reputation: 91
In C, a string is basically an array of char, an array is a pointer pointing to the first element in the array, and a pointer is the memory address of the value. Therefore:
*src1
to get the value that it is pointing to.src1++
means to +1 on the address, so you are basically moving where the pointer is pointing at.Upvotes: 0
Reputation: 50902
src1
is the pointer to the character, but we need the character itself. It's the same reason as in point 2, but the other way round.
If you write return *src1;
you simply return the character you've found, that's always c
, so your function would be pointless. You want to return the pointer to that char.
Because that's what the function is supposed to do. It returns the pointer to the first character c
found. So printing the string pointed by that pointer prints the rest of the string.
Upvotes: 2