Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a simplified version of strchr() [duplicate]

Tags:

c

Trying to create a simple function that would look for a single char in a string "like strchr() would", i did the following:

char* findchar(char* str, char c)
{
    char* position = NULL;
    int i = 0;
    for(i = 0; str[i]!='\0';i++)
    {
        if(str[i] == c)
        {
            position = &str[i];
            break;
        }
    }
    return position;
}

So far it works. However, when i looked at the prototype of strchr():

char *strchr(const char *str, int c);

The second parameter is an int? I'm curious to know.. Why not a char? Does this mean that we can use int for storing characters just like we use a char?

Which brings me to the second question, i tried to change my function to accept an int as a second parameter... but i'm not sure if it's correct and safe to do the following:

char* findchar(char* str, int c)
{
    char* position = NULL;
    int i = 0;
    for(i = 0; str[i]!='\0';i++)
    {
        if(str[i] == c) //Specifically, is this line correct? Can we test an int against a char? 
        {
            position = &str[i];
            break;
        }
    }
    return position;
}
like image 566
360NS Avatar asked Feb 03 '17 07:02

360NS


People also ask

What is Strchr () in c programming?

Description. The strchr() function finds the first occurrence of a character in a string. The character c can be the null character (\0); the ending null character of string is included in the search. The strchr() function operates on null-ended strings.

Why does Strchr take an int?

In order to preserve and reproduce the "traditional" functionality of strchr , as described above, we have no other choice but to declare the parameter of strchr as an int and explicitly convert it to char inside the function. This is exactly what you observe in the code you quoted.

What does Strchr return if not found?

The strchr() function returns a pointer to the first occurrence of c that is converted to a character in string. The function returns NULL if the specified character is not found.


1 Answers

Before ANSI C89, functions were declared without prototypes. The declaration for strchr looked like this back then:

char *strchr();

That's it. No parameters are declared at all. Instead, there were these simple rules:

  • all pointers are passed as parameters as-is
  • all integer values of a smaller range than int are converted to int
  • all floating point values are converted to double

So when you called strchr, what really happened was:

strchr(str, (int)chr);

When ANSI C89 was introduced, it had to maintain backwards compatibility. Therefore it defined the prototype of strchr as:

char *strchr(const char *str, int chr);

This preserves the exact behavior of the above sample call, including the conversion to int. This is important since an implementation may define that passing a char argument works differently than passing an int argument, which makes sense on 8 bit platforms.

like image 149
Roland Illig Avatar answered Sep 24 '22 01:09

Roland Illig