Connect with us

Function returning pointer

Discussion in 'Microcontrollers, Programming and IoT' started by madakegajya, Oct 18, 2019.

  1. madakegajya

    madakegajya

    4
    0
    Oct 11, 2019
    I'm struggling to understand how does function return pointer, and the syntax on how it should be implemented. we can have function that return pointer

    Please see below the attempt I've made so far :

    Code:
    main ()
    {
        int *pointer;
        pointer = function();
       .....
    }
    
    int  *function ()
    {
      int x = 5;
     int *pointer = &x;
    
    ..........
    
    return pointer;
    }
    here i am returning pointer which points to a local variable. suppose the variable x is stored at memory address at 2500 so the value of pointer will be 2500 and this value will be returning by function ()
     
  2. Nanren888

    Nanren888

    121
    25
    Nov 8, 2015
    Not entirely sure which aspect is causing you the problem.
    Is it the passing back of the pointer (the address)?
    In your code you create a temporary variable, "x", local to your function, and then pass this address back, as if it were still valid. As the function exits, this location becomes no longer allocated to this function's local variables.
    .
    If you search online you will find many people with similar questions and answers.
    Some suggest using malloc in the function to create a place for the variable that does not disappear on return. (Remember to free it)
    Other suggest declaring it "static".
    .
    K&R's swap(a,b) function illustrates passing pointer to safe variable into functions so they have a more permanant place to put the results.
    .
    As illustrated, creating a local variable and then passing back a pointer to where it used to be, when the function exists, does not seem to have any purpose.
    .
    Long time since I spoke C fluently, but I'd guess that if your compiler allows it, something would catch such a reference, on attempted access or throw some sort of segmentation fault.
    .
    As to your original question, a function returning a pointer, you have it explained in your question. A pointer is an address. The function returns a number of a size appropriate on the target machine to represent an address.
    .
    Have I missed your point?
     
  3. BobK

    BobK

    7,642
    1,662
    Jan 5, 2010
    The syntax is correct, but, as the first reply states, the pointer is invalid as soon as the function exits, thus all you are doing with this code is creating a bug at runtime. To return a pointer that is still good after it is returned either allocate from dynamic memory (malloc) or take the address of a global variable, not a local.

    Bob
     
    Harald Kapp and (*steve*) like this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-