passing array of type struct to function

This is a discussion on passing array of type struct to function within the C Programming forums, part of the General Programming Boards category; Code: typedef struct stA { int a,b; }; main() { stA elems[] = {{10,10},{20,20},{30,30}}; func(elems); } void func(stA *arr) { ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    11

    passing array of type struct to function

    Code:
    typedef struct stA
    {
        int a,b;
    };
    
    main()
    {
        stA elems[] = {{10,10},{20,20},{30,30}};
        func(elems);
    }
    
    void func(stA *arr)
    {
        cout << arr[0].a << endl;
    }
    Q 1) Is there a way of finding the number of array elements inside the function?
    Q 2) Is there a way of passing the elements of array in the function call itself? Something like: func({{10,10},{20,20},{30,30}}

    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    1) No, you have to pass the size separately.
    2) No.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    > Q 1) Is there a way of finding the number of array elements inside the function?
    No.
    Unless you have some kind of magic marker (like \0 for strings), and even then, that only tells you where the marker is, not how big the underlying array is.

    > Is there a way of passing the elements of array in the function call itself?
    You can do this in gcc (it's an extension), but it isn't standard C.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Q 1) Is there a way of finding the number of array elements inside the function?
    No, There is no way u could find the length of an array within the function. Either u have to send them as an extra parameter to the function or you could declare then globally such as macros or variable(not a good idea).

    Is there a way of passing the elements of array in the function call itself? Something like:
    Code:
    func({{10,10},{20,20},{30,30}}
    I don't think u can do as above. But u could send each struct element to the function

    ssharish2005

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    > Is there a way of passing the elements of array in the function call itself?
    You can do this in gcc (it's an extension), but it isn't standard C.
    I think C99 allows this:
    Code:
    struct s_t {
        int x, y;
    };
    
    void func(struct s_t) {}
    
    func((struct s_t){1, 2});
    Maybe it only works for pointers or something . . . and I have no idea if it works with arrays. Or if it's widely supported. Or maybe it is just a GCC extension.

    1) Is there a way of finding the number of array elements inside the function?
    No, but you could:
    1. Pass the number of elements in the array to the function.
    2. Use a sentenial value like '\0' to delimit the end, or set the first element to the number of elements, or something.
    3. Have a global variable or a constant that indicates the number of elements. (Sort of a variation of 1.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM

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