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

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    23

    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:
    #include<stdio.h>
    
    int fun ( int *p )
    {
    	*p = 20;
    	
    	return p;
    }
    	
    int main ()
    {
    
    
      int n;
      
      int a = 20;
      
      int *ptr =&a;
      
      n = fun (ptr);
    
    
      return 0;
    
    
    
    }



    Can somebody help me?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Maybe this instead.

    return *p;
    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.

  3. #3
    Registered User
    Join Date
    Feb 2020
    Posts
    23
    Quote Originally Posted by Salem View Post
    Maybe this instead.

    return *p;
    Code:
    #include<stdio.h> 
    int fun ( int *p )
    {
        *p = 20;
         
        return *p;
    }
         
    int main ()
    {
     
     
      int n;
       
      int a = 20;
       
      int *ptr =&a;
       
      n = fun (ptr);
      
      printf(" a address = %p \n", &a);
      printf(" a content = %d \n", a);
      
      printf(" ptr = %p \n", ptr);
      printf(" ptr content = %d \n", *ptr);
      
      printf(" ptr address = %p \n", &ptr);
    
    
      printf(" n address content = %p \n ", &n); 
      printf(" n content = %d \n", n);
     
      return 0;
      
    }
    output

    a address = 0061FF28
    a content = 20
    ptr = 0061FF28
    ptr content = 20
    ptr address = 0061FF24
    n address content = 0061FF2C
    n content = 20

    I have rewritten

    0061FF28 address a = 20
    0061FF24 address ptr = 0061FF28
    0061FF2C address n = 20


    What the function fun return ?
    20 or 0061FF28 ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's returning int, so it's 20.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2020
    Posts
    23
    Quote Originally Posted by Salem View Post
    It's returning int, so it's 20.
    but both 20 or 0061FF28 are integer

    I am trying to make a function that takes the address of the variable and returns the address.

    First example code how to get return address 0061FF28 from function ?

    return p gives warning

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Parth12
    but both 20 or 0061FF28 are integer
    Only mathematically. In this context, 20 is an int, whereas 0061FF28 is an address (i.e., the value of a pointer). The function is declared as returning an int, not a pointer.

    Quote Originally Posted by Parth12
    I am trying to make a function that takes the address of the variable and returns the address.

    First example code how to get return address 0061FF28 from function ?
    Since you want to return a pointer, declare the function as returning a pointer, i.e., change the return type to int*

    However, this also means that you should not store the return value of the function call in n since n is an int, not an int*
    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

  7. #7
    Registered User
    Join Date
    Feb 2020
    Posts
    23
    Quote Originally Posted by laserlight View Post

    However, this also means that you should not store the return value of the function call in n since n is an int, not an int*
    How to verify that function return pointer

    Code:
    #include<stdio.h> 
    
    int * fun ( int *p )
    {
        *p = 20;
          
        return p;
    }
          
    int main ()
    {
      
      
      int n;
        
      int a = 20;
        
      int *ptr =&a;
       
      printf(" a address = %p \n", &a);
      printf(" a content = %d \n", a);
       
      printf(" ptr = %p \n", ptr);
      printf(" ptr content = %d \n", *ptr);
       
      printf(" ptr address = %p \n", &ptr);
      
      return 0;
       
    }
    a address = 0061FF2C
    a content = 20
    ptr = 0061FF2C
    ptr content = 20
    ptr address = 0061FF28

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Parth12
    How to verify that function return pointer
    Check the return type of the function. Is it a pointer type? If yes, then the function returns a pointer.
    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
    Feb 2020
    Posts
    23
    Quote Originally Posted by laserlight View Post
    Check the return type of the function. Is it a pointer type? If yes, then the function returns a pointer.
    I don't have any idea how to do this

    int * p, pointer type integer

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look at this example that you yourself wrote:
    Code:
    int fun ( int *p )
    {
        *p = 20;
         
        return p;
    }
    Here is the same function, but with the place in the syntax where the return type is clearly marked:
    Code:
    LOOK_HERE_FOR_THE_RETURN_TYPE fun ( int *p )
    {
        *p = 20;
         
        return p;
    }
    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

  11. #11
    Registered User
    Join Date
    Feb 2020
    Posts
    23
    Quote Originally Posted by laserlight View Post
    Look at this example that you yourself wrote:
    Code:
    #include<stdio.h> 
     
    int *fun ( int *p )
    {
        *p = 20;
           
        return p;
    }
           
    int main ()
    {  
      int * n;
         
      int a = 20;
         
      int *ptr =&a;
      
      n = fun (ptr);
        
      printf(" a address = %p \n", &a);
      printf(" a content = %d \n", a);
        
      printf(" ptr = %p \n", ptr);
      printf(" ptr content = %d \n", *ptr);
        
      printf(" ptr address = %p \n", &ptr);
      
      printf(" n content = %p \n", n);
       
      return 0;
        
    }
    a address = 0061FF28
    a content = 20
    ptr = 0061FF28
    ptr content = 20
    ptr address = 0061FF24
    n content = 0061FF28

    Does this make sense ?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Almost. To be consistent, the last printf should be:
    Code:
    printf(" n = %p \n", n);
    Also, technically the argument corresponding to %p should be a void*, so you may see:
    Code:
    printf(" n = %p \n", (void*)n);
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: return makes integer from pointer without a cast
    By ariasukma in forum C Programming
    Replies: 9
    Last Post: 07-02-2016, 02:25 AM
  2. Replies: 6
    Last Post: 04-09-2014, 09:19 PM
  3. 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
  4. [warning] strcpy makes pointer from integer without a cast
    By HandsThatHold in forum C Programming
    Replies: 5
    Last Post: 08-14-2012, 08:16 AM
  5. Replies: 17
    Last Post: 02-13-2006, 01:19 PM

Tags for this Thread