#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2

    Smile How to use functions returning char *


    I understand pointers. They point to a location in the computers memory. What I don't understand is how to sore the return value from string functions like this one:

    char * strstr (const char *haystack, const char *needle);

    const char equals = '=';
    char *a = NULL;

    a = strstr(argv[1], equals);

    that code does not work (yes some code has been left out to make it briefer.)


    in.c: In function ‘main’:
    in.c:11:5: warning: passing argument 2 of ‘strstr’ makes pointer from integer without a cast [enabled by default]
    In file included from in.c:2:0:
    /usr/include/string.h:338:14: note: expected ‘const char *’ but argument is of type ‘char’
    in.c:11:8: warning: assignment makes integer from pointer without a cast [enabled by default]
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    The strstr() function requires two char* arguments, (C-strings) not a C-string and a char.

    You defined equals as a single char, not a nul terminated character array.

    Jim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2
    Originally Posted by jimblumberg
    The strstr() function requires two char* arguments, (C-strings) not a C-string and a char.

    You defined equals as a single char, not a nul terminated character array.

    Jim
    Thanks Jim but I'm not sure if I understood you, do you mean I do this?

    char equals[] = "=/n"

    char a = strstr(argv[1], equals);
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    That will work only if you want to search for a string that contains '=' and '/' and 'n'. If you only want to find the equal sign then maybe something like:

    Code:
    char equals[] = "=";
    Also "/n" is not the newline character, that is "\n". The '/' is just a regular character, not the "escape" character.

    Jim
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    Originally Posted by ballsystemlord
    Thanks Jim but I'm not sure if I understood you, do you mean I do this?

    char equals[] = "=/n"

    char a = strstr(argv[1], equals);
    And make "a" pointer.Not the char only.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by ballsystemlord
    Thanks Jim but I'm not sure if I understood you, do you mean I do this?

    char equals[] = "=/n"

    char a = strstr(argv[1], equals);
    To put jaysinghp's reply in more general terms: RTFM! ("Read The Manual!")

    When you read the documentation for strstr, it tells you, among other things, what data types the arguments need to be and what data type the function returns and what those return values mean. You are perfectly capable of looking up that information yourself. Don't make others give you that information by writing broken code and then asking what's wrong with it. That's the reason why that strongly worded "RTFM!" came into existence.

    What data type does strstr return? Don't you think that the variable that you declare to store that return value should be of that same data type?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2
    Originally Posted by jaysinhp
    And make "a" pointer.Not the char only.
    I should have catched that.
    Consider the question answered.

IMN logo majestic logo threadwatch logo seochat tools logo