Thread: return pointer from function

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    10

    return pointer from function

    I have read a lot of information online but still can't figure out how to return pointer pointer from function

    I made small example

    Code:
    #include <stdio.h>
    
    int* fp(void);
    
    
    void main()
    {
        int *p;
    
    
        p = fp();
    	
        printf("%d",*p);
    }
    
    
    int* fp(void)
    {
         int *q;
    	         
    	return q;
    }
    I want to get pointer from the function fp How to do that ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,455
    The problem is that the pointer is local to the function and will only be valid in that function.

    Instead of "returning" a pointer it would be better to pass the pointer into the function.

    Code:
    #include <stdio.h>
     
    void fp(int *value);
     
    int main(void)  // The function main() returns an int, not void.
    {
        int use_meaningful_variable_names;  
    
        fp(&use_meaningful_variable_names);
         
        printf("%d", use_meaningful_variable_names);
    }
     
     
    void fp(int *value)
    {              
        *value = 10;
    }

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    10
    Quote Originally Posted by jimblumberg View Post
    The problem is that the pointer is local to the function and will only be valid in that function.
    Does it make any sense ?

    function pass integer pointer and return integer pointer

    Code:
    #include <stdio.h>  
    int* fp(int *value); 
      
    int main(void)  // The function main() returns an int, not void.
    {
        int use_meaningful_variable_names;  
     
        fp(&use_meaningful_variable_names);
          
        printf("value - %d", use_meaningful_variable_names);
    	
    }
      
     // function pass integer pointer and return integer pointer 
     
    int* fp(int *value)
    {              
    
    
    	*value = 20;
    	
    	printf("value - %d \n", *value);
    	
    	printf("address of value - %p \n", value);
    	
    	return value;
    }
    when function will execute it will give result that is memory address or pointer that I want to show how to get result of function
    Last edited by skyr6546; 2 Weeks Ago at 11:01 PM.

  4. #4
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    You should read more about scopes in case you want to be returning pointers. You cannot be returning a local pointer as it remains on your stack only till the end of your function scope or things break. What you could do is allocate memory on the heap using new and return a pointer without worrying about scopes. You'd still have to deallocate using delete wherever you wish to stop having its dynamic extent.

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    1

    Thank you for help regarding this code. I didnt knew this

    Quote Originally Posted by Zeus_ View Post
    You should read more about scopes in case you want to be returning pointers. You cannot be returning a local pointer as it remains on your stack only till the end of your function scope or things break. What you could do is allocate memory on the heap using new and return a pointer without worrying about scopes. You'd still have to deallocate using delete wherever you wish to stop having its dynamic extent.
    Thank you for your help sir

  6. #6
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    Just realised this was posted on the C Programming section...

    In this case you should be using malloc and free instead of new and delete. However, in general, the programmer shouldn't be spending time thinking about memory management. There are libraries that do a lot of things for you so that you worry more about the implementation of your "actual idea" in code rather than having to think about memory management.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return pointer to a vector of objects as a function return
    By Adaptron in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2016, 09:23 AM
  2. Replies: 23
    Last Post: 11-23-2011, 12:29 PM
  3. Function syntax to return a pointer...
    By tzuch in forum C Programming
    Replies: 1
    Last Post: 05-29-2008, 07:53 AM
  4. Have function return file pointer
    By krogz in forum C Programming
    Replies: 6
    Last Post: 05-03-2007, 08:56 PM
  5. Can a function return a pointer to itself?
    By King Mir in forum C Programming
    Replies: 4
    Last Post: 04-19-2006, 01:15 PM

Tags for this Thread