Thread: return values from a function

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    6

    return values from a function

    hi,

    i have a function which i want to return two values. I used struct and pointer approach but neither did not work. here is structure approach.

    Code:
    struct myvar{
       int a,b;
    };
    
    struct myvar function();
    
    void main()
       { 
          int x,y;
          
          while(1)
          {     
         struct myvar p = function();
            x = p.a;
            y = p.b;
    
          }
    
       }
    
    struct myvar function()
       {
         int r,c;
          while(1)
          {
    ....
    ...
           
            k.r = 1;
            k.c = 2;
          
           return (k);
            
          }
    
       }
    Tried so many times but i get error- aggregate initialization needs curly braces in struct myvar p = function(); in the main( )

    can anybody help?

    thanks
    Last edited by CLearner12; 06-11-2015 at 12:32 AM. Reason: modification

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what compiler you are using - I have no problems compiling fixed code in gcc

    Code:
    struct myvar
    {
        int a,b;
    };
    
    struct myvar function();
    
    int main(void)
    {
        int x,y;
    
        struct myvar p = function();
        x = p.a;
        y = p.b;
    
        return x+y;
    }
    
    struct myvar function()
    {
        struct myvar k;
    
        k.a = 1;
        k.b = 2;
    
        return (k);
    }
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    6
    Quote Originally Posted by vart View Post
    what compiler you are using - I have no problems compiling fixed code in gcc

    Code:
    struct myvar
    {
        int a,b;
    };
    
    struct myvar function();
    
    int main(void)
    {
        int x,y;
    
        struct myvar p = function();
        x = p.a;
        y = p.b;
    
        return x+y;
    }
    
    struct myvar function()
    {
        struct myvar k;
    
        k.a = 1;
        k.b = 2;
    
        return (k);
    }
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    I am using keil compiler(c51 i think)

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Please check with the below
    Code:
    typedef struct myvar* ptr_myvar;
    ptr_myvar function();

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    6
    Quote Originally Posted by ArunS View Post
    Please check with the below
    Code:
    typedef struct myvar* ptr_myvar;
    ptr_myvar function();
    hi,

    where should i put those statements? tried in couple of places but i get erros

    thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CLearner12
    i have a function which i want to return two values. I used struct and pointer approach but neither did not work. here is structure approach.
    Are the two values related such that they are rightfully grouped in a struct? If so, then group them in a struct. Returning a struct object as demonstrated by vart in post #2 should work: have you tried to compile the code that vart posted?

    As for the "pointer approach", I would expect something like this:
    Code:
    void function(struct myvar *k)
    {
        k->a = 1;
        k->b = 2;
    }
    Of course, this means that you need to create a struct myvar object in main and pass a pointer to it.

    That said, if the two values are not related to each other such that they are rightfully grouped in a struct, then you could consider another approach, e.g., return one of them and have the other be a pointer parameter.

    Quote Originally Posted by ArunS
    Please check with the below
    Code:
    typedef struct myvar* ptr_myvar;
    ptr_myvar function();
    That is a bad idea:
    • Changing the return type to be a pointer is likely to be a wrong approach: unless the caller passes in a pointer that is then returned, dynamic memory allocation or some other solution that could be unnecessary complication must be used.
    • The pointer typedef could obfuscate the pointer here.


    Quote Originally Posted by CLearner12
    where should i put those statements? tried in couple of places but i get erros
    I suggest that you disregard ArunS's suggestion for the reasons I outlined.
    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
    Jun 2015
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Are the two values related such that they are rightfully grouped in a struct? If so, then group them in a struct. Returning a struct object as demonstrated by vart in post #2 should work: have you tried to compile the code that vart posted?

    As for the "pointer approach", I would expect something like this:
    Code:
    void function(struct myvar *k)
    {
        k->a = 1;
        k->b = 2;
    }
    Of course, this means that you need to create a struct myvar object in main and pass a pointer to it.

    That said, if the two values are not related to each other such that they are rightfully grouped in a struct, then you could consider another approach, e.g., return one of them and have the other be a pointer parameter.


    That is a bad idea:
    • Changing the return type to be a pointer is likely to be a wrong approach: unless the caller passes in a pointer that is then returned, dynamic memory allocation or some other solution that could be unnecessary complication must be used.
    • The pointer typedef could obfuscate the pointer here.



    I suggest that you disregard ArunS's suggestion for the reasons I outlined.
    hi, thanks

    but the problem is likely Keil 51 compiler. so i would be glad if there are other ways around this problem

    I have a small screenshot of what i get from the above working code in keil.

    it says aggregate initialization needs curly braces and has something to do with language construct that some C compiler won't work with.

    return values from a function-error-png

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Interesting. I tried compiling it with my Keil C51 and got the same error.

    Some research indicates that this compiler is intended to largely comply with the old C89/C90 standard.

    According to a draft of this standard:

    Quote Originally Posted by C89/C90-3.5.7
    All the expressions in an initializer for an object that has static storage duration or in an initializer list for an object that has aggregate or union type shall be constant expressions.
    So the code you posted is invalid according to that version of the standard, since you're not initializing a struct variable with a constant expression.

    This restriction seems limited to initialization and not assignment, so this should work for you:

    Code:
    struct myvar p;
    
    p = function();
    I personally would prefer the pointer approach illustrated above by laserlight.

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    6
    yes, thanks it worked now.

    i am not sure how to use the pointer, haven't tired yet

    thanks to all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-10-2013, 04:03 PM
  2. Having a function return three values
    By alanb in forum C++ Programming
    Replies: 12
    Last Post: 09-02-2009, 08:51 AM
  3. function return values
    By ashok449 in forum C Programming
    Replies: 10
    Last Post: 12-21-2007, 02:57 AM
  4. can a function return 2 values?
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 09-27-2005, 02:35 AM
  5. Can objects contain/return values like a function?
    By Panopticon in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2003, 07:44 PM

Tags for this Thread