Thread: Need help with returning pointer from function

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    2

    Need help with returning pointer from function

    Hi all! I am new here so i'm hoping this community is vivid and willing to help

    As the title says, i'm using a function which returns a pointer to a struct:

    the struct is the following:
    Code:
    typedef    struct POINT
    {
       uint16_t x;
       uint16_t y;
    }Coordinate;
    the function i'm using:

    Code:
    Coordinate * Read_XTP2046(void)
    {
    static Coordinate screen; //calculations to determine the coordinates screen.x=(temp[1]+temp[2])/2; screen.y=(temp[0]+temp[2])/2; // and so on... return &screen;
    }


    The question is: how do i catch this pointer and make it into a Coordinate struct in which i can read the x and y.

    In my main program i would do the following:

    Code:
    Coordinate cor;
    
    cor = Read_XTP2046();
    This does not work, as the function returns a pointer, but i have no idea how to transform this pointer into a Coordinate struct.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One option is to change the function to take a pointer to a Coordinate instead of having a static local variable. If that is not feasible, you could write a wrapper:
    Code:
    void Read_XTP2046_Into(Coordinate *coordinate)
    {
        Coordinate *result = Read_XTP2046();
        coordinate->x = result->x;
        coordinate->y = result->y;
    }
    
    /* ... */
    
    Coordinate cor;
    Read_XTP2046_Into(&cor);
    You could also make use of memcpy for your wrapper since the struct does not have any pointer members that need to be handled separated anyway, but that's up to you.
    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 Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    As @laserlight stated you could pass the struct as a parameter, but sometimes I had a habit to not do so. I don't know maybe because Its not exotic.
    You can use malloc:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct x
    {
        int member;
    };
    
    
    struct x* foo ()
    {
        struct x* internalStruct = malloc(sizeof(struct x));
    
    
        internalStruct->member = 1;
    
    
        return internalStruct;
    }
    
    
    int main()
    {
        struct x* ptr = foo();
    
    
        printf("%i", ptr->member);
    
    
        return 0;
    }
    Or you can use your struct as an array of members:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct x
    {
        int member;
    };
    
    
    struct x* foo ()
    {
        static struct x internalStruct[] = {1};
    
    
        return internalStruct;
    }
    
    
    int main()
    {
        struct x* ptr = foo();
    
    
        printf("%i", ptr->member);
    
    
        return 0;
    }
    Same thing. Array decays to a pointer to the first element. Structure is the same thing alrady. Thats one of the reasons why they are considered very similar.
    Last edited by Al3; 11-21-2014 at 12:31 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Al3
    You can use malloc:
    This could be a good choice if you intend to use Read_XTP2046 as the init function for your Coordinate interface. On the other hand, you should free what you malloc, and you might not always want to use malloc.

    Quote Originally Posted by Al3
    Or you can use your struct as an array of members:
    This approach has the same pitfall as your current approach: you still have a static local variable, so you can only ever have one Coordinate object unless you make a copy, and if you do not keep this in mind, you might end up overwriting a Coordinate object that you thought was a different object.
    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 Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by laserlight View Post
    This could be a good choice if you intend to use Read_XTP2046 as the init function for your Coordinate interface. On the other hand, you should free what you malloc, and you might not always want to use malloc.


    This approach has the same pitfall as your current approach: you still have a static local variable, so you can only ever have one Coordinate object unless you make a copy, and if you do not keep this in mind, you might end up overwriting a Coordinate object that you thought was a different object.
    Most of the recent compilers afaik free automatically allocated space on terminating. There are a few cases you can find this useful. But this is not a garbage collector or some form of!
    Anyway.. I thought he wants to modify and return a struct from a function. There are like 10 or maybe even more ways you can pick up to edit structures. One of the worst ways is doing that directly.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Al3
    Most of the recent compilers afaik free automatically allocated space on terminating.
    It isn't so much the compiler as the operating system, i.e., memory allocated for a process would be cleaned up when the process is terminated, even if the dynamically allocated memory was not deallocated prior to termination. However, relying on this can develop a bad habit since as you noted this is not proper garbage collection.

    Quote Originally Posted by Al3
    I thought he wants to modify and return a struct from a function.
    I think Hectickz wants to populate the cor variable using Read_XTP2046. If the aim is to modify an existing object, then the static local variable, whether as a lone object or as an array of one element, and the use of malloc, are both bad choices.

    Quote Originally Posted by Al3
    One of the worst ways is doing that directly.
    What do you mean?
    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 Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Yes, the system deallocates. But also the compiler is optimized to throw-out. Everything is lowered in the object file.

    I though he just want to take modify and return a struct pointer, noting he has his reason for returning a structure pointer instead of just structure.
    And yeah. He can do that with dynamic or static allocated pointer. If he don't do that he is fated of u/b (most certainly crash)

    I meant this:
    Code:
    (*(int *)structure+sizeof(int))
    Assuming

    • The structure member is type int


    • 2. There are no padding bytes (Use #pragma pack if there are)
    Last edited by Al3; 11-21-2014 at 01:26 PM.

  8. #8
    Registered User
    Join Date
    Nov 2014
    Posts
    2
    Hi all! thanks for the fast replies!

    Maybe some more information is in order.
    The Read_XTP2046() function reads the x and y value from the touchscreen which are analysed by the XTP2046 chip. So these variables change every time whenever I press the screen. I will try the wrapper as it seems the most easiest way to achieve my goal, simply reading were I press the screen.

    I need to add that I am clearly outskilled by all of you, and that I am not familiar with memcpy or malloc. I only program microcontrollers and this is for the LPC1788 ARM chip interfacing with an TFT LCD touch display.
    Last edited by Hectickz; 11-21-2014 at 02:06 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So long as you always know about the "must make a copy" part of the restrictions, then having a static local works well enough (see such functions as asctime(), which do the same thing).

    In your caller function, you do this.
    Coordinate cor;
    cor = *Read_XTP2046();


    But you can save yourself at least one restriction on calling the function, by returning the structure by value.
    Eg.
    Code:
    Coordinate Read_XTP2046(void)
    {
        Coordinate screen; 
        //calculations to determine the coordinates 
        screen.x=(temp[1]+temp[2])/2; 
        screen.y=(temp[0]+temp[2])/2; // and so on... 
        return screen;
    }
    
    int main ( ) {
        Coordinate cor;
        cor = Read_XTP2046();
    }
    For a small struct with only a couple of members, the compiler will likely optimise away a lot of the apparent effort.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function returning *this pointer
    By 4c0 in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2013, 05:35 AM
  2. Address off when returning pointer from function.
    By dtow1 in forum C Programming
    Replies: 3
    Last Post: 09-14-2011, 02:11 PM
  3. Returning pointer from function question
    By Rune Hunter in forum C++ Programming
    Replies: 12
    Last Post: 07-12-2007, 09:45 AM
  4. function returning pointer
    By blue_gene in forum C Programming
    Replies: 7
    Last Post: 04-19-2004, 02:35 PM

Tags for this Thread