'How to write my_strchr() in C
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
*src1in thewhileloop condition(*src1 != '\0'), is*src1a pointer to theconst char*? Can we just usesrc1instead?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?
Solution 1:[1]
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).
Solution 2:[2]
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:
- You can use
*src1to 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.- Since you are returning a pointer, it is essentially equal to a string (char array).
Solution 3:[3]
In addition to Jabberwocky's answer, please note that the code in the question has 2 bugs:
cmust be converted tocharfor the comparison with*src1:strchr("ABC", 'A' + 256)returns a pointer to the string literal unlesscharhas more than 8 bits.Furthermore, if
cconverted to acharhas the value0, a pointer to the null terminator should be returned, notNULLas 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;
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | chqrlie |
| Solution 2 | Verstand |
| Solution 3 | chqrlie |
