Passing structure field to function

This is a discussion on Passing structure field to function within the C Programming forums, part of the General Programming Boards category; Hi, suppose I've a structures like this Code: typedef struct { int f[5]; } foo; filled, for example: Code: foo[0].f[0] ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Passing structure field to function

    Hi,

    suppose I've a structures like this

    Code:
    typedef struct
    {
      int f[5];
    } foo;
    filled, for example:

    Code:
    foo[0].f[0] = 1
    foo[0].f[1] = 2
    foo[0].f[2] = 3
    foo[0].f[3] = 4
    foo[0].f[4] = 5
    foo[1].f[0] = 2
    foo[1].f[1] = 4
    foo[1].f[2] = 6
    foo[1].f[3] = 8
    foo[1].f[4] = 10
    foo[2].f[0] = 3
    foo[2].f[1] = 6
    foo[2].f[2] = 9
    foo[2].f[3] = 12
    foo[2].f[4] = 15
    In this way, I have a table, how to pass to function column 1 (values 2,4,6) so I can use it as an array ?

    Thanks.
    Last edited by Lude; 01-18-2008 at 05:17 AM. Reason: error!

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    You can:

    a) Pass the array directly:

    Code:
    
    // ..
    somefunction(foo[1].f); // prototype: somefunction(int *); 
    
    // ..
    b) Pass the structure as a pointer(Best method if you want to use other member of the struct)

    Code:
    foo bar;
    
    somefunction(&bar); // Prototype: somefunction(foo *bar);
    or c) Pass the struct directly - very inefficient so I won't even display code for that.
    Last edited by IceDane; 01-18-2008 at 06:56 PM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Thank you, I've edit my post, there's was an error!
    I want column as array, not structure field.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    You would have to pass the whole array (arrays are passed as pointers, so it's no biggie), then subscript through the data in the manner you want to.
    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.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    There's no other way ?
    So I've to duplicate function, one to accept struct and one to accept array.
    I've some values into struct and other into arrays.

    (I'm thinking that is not possible because I've no contiguos memory block but I can wrong)
    Last edited by Lude; 01-18-2008 at 02:10 PM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    What's the problem? Why won't a single function do for all, one that takes a struct or one that just takes an array?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. passing structure variables into function
    By sarathius in forum C Programming
    Replies: 1
    Last Post: 04-07-2008, 11:56 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  5. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 05:16 PM

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