string ptr help

This is a discussion on string ptr help within the C Programming forums, part of the General Programming Boards category; PHP Code: char StringPtr (const  char  * str ){ /* goes through a string and returns a pointer to the first space else NULL */      while(* str  !=  ' '  && * str  !=  '

Thread: string ptr help

' ){          ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    string ptr help

    PHP Code:
    char StringPtr(const char *str){
    /* goes through a string and returns a pointer to the first space else NULL */
        
    while(*str != ' ' && *str != '\0'){
            
    str++;
        if (*
    str == '\0')
            return 
    NULL;
        else
            return 
    str;
        
    //*str == '\0' ? return NULL : return str;
        
    }

    the problem is I get the following warnings on DEVc++
    C:/Dev-Cpp/Ccode/ch11-strings/447no9and10.c:27: warning: return makes integer from pointer without a cast

    C:/Dev-Cpp/Ccode/ch11-strings/447no9and10.c:29: warning: return makes integer from pointer without a cast

    Execution terminated
    of course when i typecast NULL to type int and return *str. it doesn't complain. Is this just being pedantic, or is there some legitamate reason.


    [edit]and why am i getting a parse error on the following[/edit]
    Code:
    *str == '\0' ? return NULL: return str;
    Last edited by caroundw5h; 08-11-2004 at 09:59 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    you are returning str which is a const char*, your function declaration says your going to return a char.

    [edit]and why am i getting a parse error on the following[/edit]
    Code:
    *str == '\0' ? return NULL: return str;
    you probably want:

    return (*str == '\0' ) ? NULL : str;

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    thanks for the reply. I still get the following error though
    21 C:\Dev-Cpp\Ccode\ch11-strings\447no10.c
    [Warning] return makes integer from pointer without a cast

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Did you remember to change the prototype and the function header from
    Code:
    char StringPtr(const char *str){
    to
    Code:
    char * StringPtr(const char *str){

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    I guess you dont define *str in your fun()?

    PHP Code:
    char StringPtr(const char *str)

        
    char  *str         // add this statement, I guess.

        
    while(*str != ' ' && *str != '\0'){ 
            
    str++; 
        if (*
    str == '\0'
            return 
    NULL
        else 
            return 
    str
        
    //*str == '\0' ? return NULL : return str; 
        



  6. #6
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by toysoldier
    I guess you dont define *str in your fun()?

    Code:
    char StringPtr(const char *str)
    { 
        char  *str         // add this statement, I guess.
    Please stop saying insanities. str is already a parameter.
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ Emmanuel Delaha :
    Sorry for my stupid answer. forgive me.

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thanks for the replies guys: this is my code and these are my errors. maybe you can spot something i can't
    PHP Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define STRING "THIS IS JUST A TEST"
    char StringPtr(const char *str);

    int main(void){

    char StringPtr(const char *str){
    /* goes through a string and returns a pointer to the first space else NULL */
        
    while(*str != ' ' && *str != '\0'){
            
    str++;
        if (*
    str == '\0')
            return 
    NULL;
        else
            return 
    str;
        }
    }

    return 
    0;

    errors
    C:/Dev-Cpp/Ccode/ch11-strings/447no10.c: In function `StringPtr':
    C:/Dev-Cpp/Ccode/ch11-strings/447no10.c:15: warning: return makes integer from pointer without a cast
    C:/Dev-Cpp/Ccode/ch11-strings/447no10.c:17: warning: return makes integer from pointer without a cast
    funny thing is. the example in my book has this same code. and not that it's infalliable (since i've spotted a couple of errors) but you'd think still it'd be correct.

    when i attempt to fix it
    PHP Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define STRING "THIS IS JUST A TEST"
    char *StringPtr(const char *str);

    int main(void){

    char *StringPtr(const char *str){//now a ptr to char on return
    /* goes through a string and returns a pointer to the first space else NULL */
        
    while(*str != ' ' && *str != '\0'){
            
    str++;
        if (*
    str == '\0')
            return 
    NULL;
        else
            return *
    str;
        }
    }

    return 
    0;

    I still get the same error mesage except only one this time. and it points to the
    Code:
    return *str
    line. is this something with DevC++ or what?
    Last edited by caroundw5h; 08-12-2004 at 10:16 AM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    try return str;
    I know you read alot on pointers. Remember your functions returns a pointer to type char, not the value of pointer to type char.

  10. #10
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by linuxdude
    try return str;
    I know you read alot on pointers. Remember your functions returns a pointer to type char, not the value of pointer to type char.
    thanks, but that was the first attempt I made returning its addy. As you can see from the above post.

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    so you got it right.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well one problem I see is that the StringPtr function is being defined INSIDE of main(). Nested functions are not allowed in standard C

    as to the error:
    Code:
    return *str;
    This returns what str is pointing to which is a character
    Code:
    return str;
    This returns the address of what str is pointing to.

    Personal Preference Alert: Use code tages instead of php tags. The colors in the php tag make it harder to read.

    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <ctype.h> 
    #define STRING "THIS IS JUST A TEST" 
    char *StringPtr(const char *str); 
    
    int main(void){ 
      /* Call StringPtr() in here */
    return 0; 
    }
    
    
    char *StringPtr(const char *str){
        while(*str != ' ' && *str != '\0'){ 
            str++; 
        if (*str == '\0') 
            return NULL; 
        else 
            return str; 
        } 
    }
    Last edited by Thantos; 08-12-2004 at 11:41 AM.

  13. #13
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by caroundw5h
    Thanks for the replies guys: this is my code and these are my errors. maybe you can spot something i can't
    Sure. Don't define a function inside a function. It's not a standard C feature. It's a gcc extension. Don't do that. Never.
    Code:
    char StringPtr(const char *str){
            return NULL;
            return str;
    }
    You pretend that you return a char, but actually, you have returned an address. Be more consistent:
    Code:
    char *StringPtr(const char *str){
    funny thing is. the example in my book has this same code.
    If so, get your money back an buy a true C book.
    when i attempt to fix it
    Code:
    char *StringPtr(const char *str){//now a ptr to char on return
    /* goes through a string and returns a pointer to the first space else NULL */
        while(*str != ' ' && *str != '\0'){
            str++;
        if (*str == '\0')
            return NULL;
        else
            return *str;
        }
    }
    I still get the same error mesage except only one this time. and it points to the
    Code:
    return *str
    line. is this something with DevC++ or what?
    I vote for 'what', or maybe 'who'

    You pretend that you return the address of a char, but actually, you returned a char. Once again, be more consistent.
    Last edited by Emmanuel Delaha; 08-12-2004 at 12:00 PM.
    Emmanuel Delahaye

    "C is a sharp tool"

  14. #14
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    so from what i'm gathereing my first code was right?
    Code:
     char StringPtr(const char *str){
    /* goes through a string and returns a pointer to the first space else NULL */
        while(*str != ' ' && *str != '\0'){
            str++;
        if (*str == '\0')
            return NULL;
        else
            return str;
       
        }
    }
    this returns the address. but I still get the warnings as I got like in my first post.

  15. #15
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by caroundw5h
    so from what i'm gathereing my first code was right?
    Code:
     char StringPtr(const char *str){
            return NULL;
            return str;
    }
    this returns the address. but I still get the warnings as I got like in my first post.
    Please learn to read. As mentionned several times, the return type must be 'char*' and not 'char'...
    Code:
    char *StringPtr(const char *str){<...>
    Emmanuel Delahaye

    "C is a sharp tool"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with unaligned intrinsics
    By The Wazaa in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2009, 12:36 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21