Home > Back-end >  How to write my_strchr() in C
How to write my_strchr() in C

Time:02-08

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:

  1. 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?

  2. When we return value and src1 , we do not have that *src1, why?

  3. 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?

CodePudding user response:

  1. 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.

  2. 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.

  3. 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.

CodePudding user response:

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:

  1. You can use *src1 to get the value that it is pointing to.
  2. src1 means to 1 on the address, so you are basically moving where the pointer is pointing at.
  3. Since you are returning a pointer, it is essentially equal to a string (char array).

CodePudding user response:

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;
        }
    }
}

CodePudding user response:

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 operatior !=) the end of string indicator (NULL or \0), do some stuff, then more 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.

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_strch(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 or NULL).

  •  Tags:  
  • Related