-3

My assignment is to write my own version of strchr, yet it doesn't seem to work. Any advice would be much appreciated. Here it is:

char *strchr (const char *s, int c) //we are looking for c on the string s
{

    int dog; //This is the index on the string, initialized as 0
    dog = 0;
    int point; //this is the pointer to the location given by the index
    point = &s[dog];
    while ((s[dog] != c) && (s[dog] != '\0')) { //it keeps adding to dog until it stumbles upon either c or '\0'
            dog++;
            }
    if (s[dog]==c) {
            return point; //at this point, if this value is equal to c it returns the pointer to that location
            }
    else {
            return NULL; //if not, this means that c is not on the string
            }
}
Mat
  • 202,337
  • 40
  • 393
  • 406

4 Answers4

4

You return "point" which was originally initialized to beginning of string and not moved since then. You don't need that variable at all, but could simply return &s[dog] (although I would prefer something more descriptive than dog as a variable name).

In fact you would survive with something as simple as this:

while (*s != c && *s)
    ++s;

return (*s == c) ? s : NULL; 
Jari
  • 2,152
  • 13
  • 20
4

You are trying to store an address into point but it's an int-variable. You should do something like this:

char *strchr(char *s, char c) {
    int pos = 0;
    while (s[pos] != c && s[pos] != '\0')
        pos++;
    if (s[pos] == c)
        return &s[pos];
    else
        return NULL;
}

By the way: s should be char * not const char * because your returning an pointer to achar, and that wouldn't be a good style ;) (or return const char *)

qwertz
  • 14,614
  • 10
  • 34
  • 46
  • 1
    The C standard mandates that `strchr` takes `const` pointer but returns a mutable one into the same buffer. So a compliant pure-C implementation has to cast the `const` away. – Fred Foo Jul 08 '12 at 21:51
  • You can also increment the pointer directly like this: http://pastebin.ubuntu.com/1081902/ – con-f-use Jul 08 '12 at 22:05
  • You should also use type `size_t` for `pos` rather than `int`. – caf Jul 09 '12 at 00:44
0
int point;

This is not the declaration of a pointer, here is how to declare a pointer to an int:

int *bla;

In your case &s[dog] is a pointer to a const char, so you want to declare point this way:

 const char *point;

And as others pointed out, you are actually ignoring this pointer in your function afterwards.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

Here in your code

int point; //this is the pointer to the location given by the index
point = &s[dog];

you are trying to convert a pointer to char to an int, when

char* point = &s[dog];

is what you want. You should have seen this from the return type of your function. You want to return a char* but you return an int (your variable point) or NULL. As you never actually change point, you are actually returning the address of the first character in your array, so your code is not working as you would like anyway. If you insist on this you would be better using

char* point = &s[dog];
while ((*point != c) && (*point != '\0')) {          
   ++point;
}
return (*point == c) ? point : NULL;

But here it looks like you still have a conceptual problem as you want to compare a char with an int. You should work out if you need an int array or a char array. If you want a char array, change your input argument c to be of type char.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101