关于C语言strchr函数int c参数与const char指针的疑问咨询
strchr Function in Your Code Hey there! Let's break down this strchr function step by step—pointers and that int c parameter can feel confusing at first, but once we unpack it, it'll make sense.
What does strchr do?
This custom implementation of strchr has one clear job: search through the string pointed to by s, and return a pointer to the first occurrence of the character represented by c. If it never finds the character, it returns 0 (which is equivalent to NULL in C, meaning no match exists).
Why is c an int instead of a char?
This is the tricky part for many new programmers! Here's the breakdown:
- In C, characters are just small integers under the hood—every character maps to an ASCII value (like
'a'is 97,'z'is 122). When you pass acharto a function expecting anint, thechargets automatically promoted to anint(the value stays the same, it's just stored in a larger type). - There's a historical reason too: the standard library's official
strchrallows passingEOF(a special value equal to-1) as thecparameter. SinceEOFis outside the range of a signedchar(which typically goes from-128to127), using anintensures we can store that value without truncation. For your code's use case though, you're just passing regular characters, so you can think ofcas the ASCII value of the character you're searching for.
Walking through the strchr code line by line
Let's look at the implementation again to connect all the pieces:
char *strchr(register const char *s, int c) { do { if (*s == c) { return (char*)s; } } while (*s++); return (0); }
register const char *s: Theconstmeans we won't modify the stringspoints to (we're only reading it), andregisteris a hint to the compiler to store this pointer in a CPU register for faster access (you can ignore this part for now—it's just an optimization).do { ... } while (*s++);: This loop runs through every character in the string:*sgives us the current character the pointersis pointing to. When we compare*s == c, thecharvalue of*sgets promoted to anint, so we're comparing the ASCII value of the current character to the integer value ofc.- If they match, we cast
s(aconst char*) to achar*and return it—this gives us a pointer directly to the matching character in the string. *s++increments the pointersto point to the next character, and checks if the old character was not the null terminator ('\0', which has an ASCII value of 0). The loop stops when we hit this null terminator (the end of the string).
- If the loop finishes without finding a match, we return
0(signaling no match was found).
How this connects to your firstmatch function
In firstmatch, when you call strchr(s2, *temp), you're passing:
s2: The string you want to search through.*temp: The current character froms1that you want to find ins2. Thischargets promoted to anintwhen passed tostrchr, which perfectly matches theint cparameter.
Quick side note: I noticed a small bug in your firstmatch function—it's declared to return a char, but you're returning temp which is a char* (a pointer). That will cause compiler errors, so you'll want to fix that if you're going to use this code!
内容的提问来源于stack exchange,提问作者MrJonesIsCountingCrows

