Struct passing to and from functions!

This is a discussion on Struct passing to and from functions! within the C Programming forums, part of the General Programming Boards category; Hello all, sorry if this question seems stupid, I've just started using C and I'm far more used to the ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    4

    Struct passing to and from functions!

    Hello all,
    sorry if this question seems stupid, I've just started using C and I'm far more used to the syntax of Matlab. I'd like to have a set of functions that perform operations on struct's that contain two elements and then return a struct that also contains two elements.

    Here is the code but I'm receiving multiple errors and don't really know when to start debugging even though the code is very short.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    typedef struct { int real; int imag;} int_type;
    
    int main()
    {
    
    int_type f, g, y, a, b, c;
    
      f.real=4;
      f.imag=7;
      g.real=21;
      g.imag=77;
    
      y=int_add(f,g);
    
      //  printf("%d and %d", y[1], y[2]);
    
      getchar();
      return 0;
    }
    
    int_type int_add(a, b)
    {
      struct int_type c;
    
      c.real = a.real + b.real;
      c.imag = a.imag + b.imag;
    
      return c;
    }
    Thanks for any help in advance.

    Seb

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have to know about a function before you can call it. This is why function prototypes were invented -- you should have the line
    Code:
    int_type int_add(int_type a, int_type b);
    before your main function, so that main knows that such a function exists. Also note that you must tell the compiler what kind of things the function parameters are.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    In addition to tabstop's observation, and implicit point that you need to indicate what a and b are in your function prologue, you need to use pointers when you are passing structs:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    typedef struct { int real; int imag;} int_type;
    
    int_type int_add(int_type *a, int_type *b);    
    
    int main()
    {
    
    int_type f, g, y, a, b, c;
    
      f.real=4;
      f.imag=7;
      g.real=21;
      g.imag=77;
    
      y=int_add(&f,&g);
    
      //  printf("%d and %d", y[1], y[2]);
    
      getchar();
      return 0;
    }
    
    int_type int_add(int_type *a, int_type *b)    
    {
      int_type c;
    
      c.real = a->real + b->real;
      c.imag = a->imag + b->imag;
    
      return c;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    In addition to tabstop's observation, and implicit point that you need to indicate what a and b are in your function prologue, you need to use pointers when you are passing structs:
    WTH are you talking about? Of course you don't need to use pointers when passing structs.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    WTH are you talking about? Of course you don't need to use pointers when passing structs.
    Oh yeah, look at that

    Well, maybe it's just me then. I guess all the OP had to do was add the prototype.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    The whole reason I wish to use struct's is to get away from the use of pointers. I just don't understand why C doesn't allow a function to output more than a single data type from a function as standard.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sebpinski View Post
    The whole reason I wish to use struct's is to get away from the use of pointers. I just don't understand why C doesn't allow a function to output more than a single data type from a function as standard.
    I am very sorry and you will know how to take my advice in the future.

    Up until a few minutes ago I would have sympathized.

    ps. pointers are great tho, you shouldn't be trying to get away from them!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    There was no problem with your advice, it was definitely welcome. I was just expressing my gripes with the whole of C. No offense intended...

    Still having problems, there appears to be an issue with the function itself now, the only error I'm receiving states:

    error: incomplete type is not allowed
    struct int_type c

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sebpinski View Post
    The whole reason I wish to use struct's is to get away from the use of pointers. I just don't understand why C doesn't allow a function to output more than a single data type from a function as standard.
    As far as I know, there are only a few languages that DO support returning more than one value: PHP, Perl and Python. All of which are interpretative rather than compiled languages.

    Just bear in mind that NOT using pointers when returning a struct means that the struct is copied both when passing it into the function, when you do the return statement (copy from the temporary variable to the return location specified by the calling function [1]) and when the fucntion returns from the temporary location to the result variable.

    [1] Returning structs [at least if they are larger than what can be put into register(s) by the normal calling conventions is done essentially as this:
    original code:
    Code:
    int_type int_add(int_type a, int_type b) 
    {
      int_type c;
    ...
       return c;
    }
    will be done somewhat like this:
    void int_add(int_type a, int_type b, int_type *temp)
    {
    int_type c;
    ...
    // return c;
    memcpy(temp, &c, sizeof(c));
    }
    [/code]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    doesn't matter, just needed to remove the 'struct' from the function text.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sebpinski View Post
    There was no problem with your advice, it was definitely welcome. I was just expressing my gripes with the whole of C. No offense intended...
    We could throw bottles at that train together, but to be fair I would feel like an abusive lover afterwards.

    And -- to make sure we're on the same page and that you didn't read just the last post instead of the last N posts -- I was wrong. You can pass a whole struct.

    But like matsp says, using pointers is kind of advantageous. The syntax may be frustrating if you're coming from something like perl and didn't make much use of references
    Last edited by MK27; 10-29-2008 at 11:49 AM.

  12. #12
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Quote Originally Posted by sebpinski View Post
    The whole reason I wish to use struct's is to get away from the use of pointers. I just don't understand why C doesn't allow a function to output more than a single data type from a function as standard.
    You should probably just quit C now.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21