Thread: Function of char* type

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    90

    Function of char* type

    I am still suffering with pointers.

    // main
    Code:
    int index = 50;
    char* flagx = mytable_get_flag(conn, index);
    printf("flag=%s\n", flagx);
    // functon in other file
    Code:
    char* mytable_get_flag(DBconn* conn, int index)
    {
        int retval = 0;
        
        // some code
        
        if (DBrows(result) > 0)
            retval = *DBgetvalue(result, 0, 0);
    
        return retval;
    }
    I am getting warning: "return makes pointer from integer without a cast" at return retval in function.

    How would I get my char* out to print value of this string in main?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You told the compiler you would be returning a char *, why are you trying to return an int?

    Jim

  3. #3
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    What's DBgetvalue()'s return value? If it creates a string and returns it, then you should define retval as char * and then...

    Code:
    ...
        if (DBrows(result) > 0)
              retval = DBgetavlaue(result, 0, 0);
          return retval;
    }

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Yes, yes, thank you.

    Code:
     char* retval = 0;
    I declared retval as int instead of char* (because of copying from other function).
    Now this work as expected.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    No it is wrong. What is 0?

    Code:
     char * retval = 0 ;
    Is really dangerous. You should initialize with some valid adress not with an unknown address.... 0 is an unknown address I can't understand what you want to do

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Mr. Lnx, 0 is default value which will be returned if no data from database.
    Should I set it to NULL insead, or what?

  7. #7
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    You are welcome

    Also consider changing char *retval = 0; to char *retval = NULL; and check in the caller that the function has NOT returned NULL before attempting to print its returned string

  8. #8
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by Mr.Lnx View Post
    No it is wrong. What is 0?

    Code:
     char * retval = 0 ;
    Is really dangerous. You should initialize with some valid adress not with an unknown address.... 0 is an unknown address I can't understand what you want to do
    0 is NULL in C++ but in C is most commonly defined as (void *)NULL

  9. #9
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    ah yes... sorry.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Interesting situation!
    DBgetvalue(result, 0, 0); is actually a string (one char) but that string is number 1-4.
    So if I set retval to "-1" then I haven't check if function return's null.
    Instead I can check atoi() of returned char and if > 0 then something useful is returned.

    Is this correct?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by migf1
    0 is NULL in C++ but in C is most commonly defined as (void *)NULL
    I think you mean: "NULL is often defined as 0 in C++, but in C it is most commonly defined as (void*)0". (Though I'm curious as to whether you really have proof of the latter claim.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by laserlight View Post
    I think you mean: "NULL is often defined as 0 in C++, but in C it is most commonly defined as (void*)0". (Though I'm curious as to whether you really have proof of the latter claim.)
    The Open Group Base Specifications Issue 7 - IEEE Std 1003.1, 2013 Edition
    OpenBSD
    FreeBSD
    NetBSD
    mingw
    tinycc

    Locally on my desktop PC:

    lcc-win32: stdio.h
    Code:
    ...
    /* Define NULL pointer value */
    #ifndef NULL
    #define NULL    ((void *)0)
    #endif
    ...
    pelles-c: stdio.h
    Code:
    ...
    /* macros */
    #ifndef NULL
    #define NULL  ((void *)0)
    #endif
    ...
    Visual C++ 2010: stddef.h
    Code:
    ...
    /* Define NULL pointer value */
    #ifndef NULL
    #ifdef __cplusplus
    #define NULL    0
    #else
    #define NULL    ((void *)0)
    #endif
    #endif
    ...
    Last edited by migf1; 06-06-2013 at 09:12 PM.

  13. #13
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Oh I forgot, my suggestion to OP for using NULL instead of 0 was just for readability reasons. The compiler is happy both ways.
    But are you aware of any C++ implementation where NULL is defined to something different than plain 0? Just curious because you said "often".
    Last edited by migf1; 06-06-2013 at 09:31 PM.

  14. #14
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    @laserlight:

    Oh my, I just saw why you asked me that question in the 1st place!
    I have a huge typo in my original post. I meant to write that in C NULL is most commonly defined as (void *)0 ( and not as (void *)NULL as I wrote there).

    Btw, in C++ NULL is always defined as plain 0.

    I'm really sorry about that!
    Last edited by migf1; 06-07-2013 at 08:31 AM.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by migf1 View Post
    Btw, in C++ NULL is always defined as plain 0.
    You're digging your hole deeper because, no, it's not.

    A pointer with value zero and the NULL pointer will compare equal. That is not the same as NULL being defined as zero. In either C or C++.

    As laserlight said, it is common in C++ for NULL to be defined as zero, and in C for it to be defined as (void *)0. But "is common" is not the same as "is always".

    It is somewhat common practice in C++ to use a value of zero instead of the NULL macro. (That has changed, or is changing, with C++11, due to introduction of the nullptr keyword. I'll leave that note there, as this is a C forum, not a C++ forum).

    You need to get your head around the notion that two distinct values can compare as equal, if there is a separate rule that specifies they are equal. The C standard has such a rule.

    You might also want to look at this and this and this.
    Last edited by grumpy; 06-07-2013 at 03:30 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'char' integer type or character type?
    By password636 in forum C Programming
    Replies: 5
    Last Post: 09-26-2012, 09:50 AM
  2. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  3. Replies: 2
    Last Post: 11-23-2011, 12:30 PM
  4. Replies: 17
    Last Post: 03-06-2008, 02:32 PM
  5. Converting type string to type const char*
    By rusty0412 in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 05:59 PM