简体   繁体   English

C中my_strchr()怎么写

[英]How to write my_strchr() in C

Right now I hope to write my own my_strchr() in the C language.现在我希望用 C 语言编写我自己的my_strchr()

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* ?为什么我们在while循环条件(*src1 != '\0')中使用*src1 src1 , *src1是指向const char*的指针吗? Can we just use src1 instead?我们可以只使用src1吗?

  2. When we return value and src1++ , we do not have that *src1 , why?当我们返回 value 和src1++时,我们没有*src1 ,为什么?

  3. For this function, it in fact prints the whole string after the required characters, but why is that?对于这个 function,它实际上在所需字符后打印了整个字符串,但这是为什么呢? Why does it not print only the character that we need to find?为什么它不只打印我们需要查找的字符?

  1. src1 is the pointer to the character, but we need the character itself. src1指向角色的指针,但我们需要角色本身。 It's the same reason as in point 2, but the other way round.这与第 2 点中的原因相同,但相反。

  2. If you write return *src1;如果你写return *src1; you simply return the character you've found, that's always c , so your function would be pointless.您只需返回找到的字符,它始终是c ,因此您的 function 将毫无意义。 You want to return the pointer to that char.您想要返回指向该字符的指针。

  3. Because that's what the function is supposed to do.因为那是 function 应该做的。 It returns the pointer to the first character c found.它返回指向找到的第一个字符c的指针。 So printing the string pointed by that pointer prints the rest of the string.因此打印该指针指向的字符串会打印该字符串的 rest。

It's important here to remember that in C a string is a series of characters that ends with a null ( '\0' ) character.重要的是要记住,在 C 中,字符串是以 null ( '\0' ) 字符结尾的一系列字符。 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.当我们将字符串作为参数传递给 function 时,我们真正得到的是指向字符串中第一个字符的指针。

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 ++ ).是 C 中非常常见的习语。我们可以将其用英语表述为: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 (空字节, 0'\0' ),做一些事情,然后移动指针指向字符串中的下一个字符(增量运算符++ )。

We often use the same kind of code structure to process other arrays or linked lists of things, comparing pointers to the NULL pointer.我们经常使用同一种代码结构来处理其他arrays或事物的链表,将指针与NULL指针进行比较。

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 .对于问题 #2,我们从这个 function src1返回指针的值而不是它指向*scr1的值,因为这个 function 应该回答的问题是“字符串中字符c的第一个实例在哪里从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:问题 #3 意味着调用这个 function 的代码正在打印一个字符串,该字符串从从这个 function 返回的指针开始。我的猜测是代码看起来像这样:

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). printf()和朋友将从参数列表中提供的与%s格式说明符匹配的位置打印,然后继续打印,直到它到达字符串字符的结尾( '\0' , null 终止符)。

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. C中,字符串基本上就是一个char数组,数组是指向数组第一个元素的指针,一个指针就是值的memory地址。 Therefore:所以:

  1. You can use *src1 to get the value that it is pointing to.您可以使用*src1获取它指向的值。
  2. src1++ means to +1 on the address, so you are basically moving where the pointer is pointing at. src1++意味着在地址上+1,所以你基本上是在指针指向的地方移动。
  3. Since you are returning a pointer, it is essentially equal to a string (char array).由于您要返回一个指针,因此它本质上等于一个字符串(字符数组)。

In addition to Jabberwocky 's answer, please note that the code in the question has 2 bugs:除了Jabberwocky的回答,请注意问题中的代码有 2 个错误:

  • 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. c必须转换为char以便与*src1进行比较: strchr("ABC", 'A' + 256)返回指向字符串文字的指针,除非char的长度超过 8 位。

  • 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.此外,如果转换为charc的值为0 ,则应返回指向 null 终止符的指针,而不是发布的代码中的NULL

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM