Thread: Passing structure field to function

  1. #1
    Registered User
    Join Date
    Jan 2008

    Passing structure field to function


    suppose I've a structures like this

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

    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 ?

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

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

    a) Pass the array directly:

    // ..
    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)

    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 07:56 PM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

  5. #5
    Registered User
    Join Date
    Jan 2008
    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 03:10 PM.

  6. #6
    Join Date
    Oct 2007
    Inside my computer
    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, 05:45 PM
  4. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  5. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 06:16 PM