Thread: warning: return makes integer from pointer without a cast

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    4

    warning: return makes integer from pointer without a cast

    Hi,

    I have an error warning: return makes integer from pointer without a cast. My code below


    Code:
    int search_dataCode(char codeValue[], t_asrepdb *r_asrepdb)
    {
    }
    char dbValue(char *codeValue)
    {
        t_asrepdb r_asrepdb;
        t_asrepdb c_asrepdb;
        char *a;
        int search_code;
        search_code = search_dataCode(codeValue, &r_asrepdb); 
        if (search_code == -1)
        {
            printf("\nThere is no code at mrepdb\n");
            fflush(stdin); getchar();
    }
        a = strcpy(r_asrepdb.value, c_asrepdb.value);
        return a;
    }

    Can somebody help me?
    Best Regards,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,417
    Notice that you declared dbValue as returning a char, but you returned a, which is declared as a pointer to char.

    By the way, note that fflush(stdin) results in undefined behaviour, though it may discard what is in the standard input stream on some implementations.
    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

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    4
    Hi Laserlight,

    I have change to below, and still error warning: return makes integer from pointer without a cast.

    Code:
    char dbValue(char *codeValue) 
    {
    	t_asrepdb r_asrepdb;
    	t_asrepdb c_asrepdb;
    	char a;
    	int search_code;
        	search_code = search_dataCode(codeValue, &r_asrepdb); 
        	if (search_code == -1)
    	{
    		
        	}
    	fgets(c_asrepdb.value, 201, stdin);
    	a = strcpy(r_asrepdb.value, c_asrepdb.value);
    	return a;
    }
    actually content a is directory like '/app/data'

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,417
    Now a is a char, but strcpy returns a pointer to char, yet you are assigning the return value of strcpy to a.

    Why is the significance of the return value of dbValue anyway?
    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

  5. #5
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    In your first code you had char *a which got its value from a call to strcpy() and at the end dbvalue() returns the value of a. So you should have changed the type of dbvalue() into char pointer.
    Like:
    Code:
    char *dbValue(char *codeValue)
    
    {
    
        ---
        ---
         char *a;
    
        ---
        ---
        a = strcpy(r_asrepdb.value, c_asrepdb.value);
        return( a);
        // after the strcpy() you could return(c_asrepdb.value) or return(r_asrepdb.value) and not use char *a at all
    
    
    }

  6. #6
    Registered User
    Join Date
    Jun 2016
    Posts
    4
    @ ddutch, Its not error when compiled but when I run it error Segmentation fault (core dumped).

    @ laserlight, I need return value. That return value content a directory and I will use that directory at other function.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,505
    Assuming you now have something like this, you have another problem.
    Code:
    char *dbValue(char *codeValue)
    {
        t_asrepdb r_asrepdb;
        t_asrepdb c_asrepdb;
        char a;
        int search_code;
            search_code = search_dataCode(codeValue, &r_asrepdb);
            if (search_code == -1)
        {
             
            }
        fgets(c_asrepdb.value, 201, stdin);
        a = strcpy(r_asrepdb.value, c_asrepdb.value);
        return a;
    }
    Firstly, the fgets line should read something like
    Code:
    if ( fgets(c_asrepdb.value, sizeof(c_asrepdb.value), stdin) ) {
      // success
    }
    1. Always test the return result of file operations.
    2. Never use magic numbers to indicate the size of buffers. Always use a sizeof(), or the #define constant.

    The return result of strcpy is the pointer you passed to the destination for the copy - namely r_asrepdb.value.
    But r_asrepdb.value is just a local variable.
    So when the function returns, the local variable no longer exists, and the pointer to the local variable is now an invalid pointer.

    This will cause you problems.

    The way out is to do something like
    Code:
    char *dbValue(char *codeValue, char *answer, size_t maxAnswerLen) {
    }
    In other words, you store the output string in answer, making sure to write no more than maxAnswerLen bytes to answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,417
    Quote Originally Posted by ddutch
    In your first code you had char *a which got its value from a call to strcpy() and at the end dbvalue() returns the value of a. So you should have changed the type of dbvalue() into char pointer.
    That would fix the type errors, but as ariasukma observed, it is not necessarily a correct fix to the overall problem because the pointer cannot point to a non-static local variable otherwise the caller might use a pointer to an object that no longer exists, but we see that the strcpy call involves r_asrepdb and c_asrepdb, both of which are non-static local to the function.
    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

  9. #9
    Registered User
    Join Date
    Jun 2016
    Posts
    4
    All, Done perfect.
    Thanks Salem, laserlight and ddutch.

  10. #10
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    I don't know why a return would make an integer without a cast unless you have something like this

    Code:
    int returnvalue()
    {
    ...
    return(valuereturning);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-09-2014, 09:19 PM
  2. warning: return makes pointer from integer without a cast
    By csharp100 in forum C Programming
    Replies: 9
    Last Post: 09-21-2013, 01:56 PM
  3. Replies: 4
    Last Post: 03-10-2009, 10:29 AM
  4. Replies: 17
    Last Post: 02-13-2006, 01:19 PM
  5. Replies: 3
    Last Post: 01-14-2002, 12:13 PM

Tags for this Thread