Thread: arrays of Structures called in a function

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    arrays of Structures called in a function

    hello all,

    i have a problem i can't get around,
    for example if ,
    i have a function:

    void function1 ( int k []);

    i can use it in the following manner:

    int h[][];

    function1(h[]); // so basically i'm passing an array.

    so now what if i have a structure

    structure1{

    int value;
    } struct[][];

    so now i can't use

    function1 ( struct[x].value) // here i'm trying to pass the an array that is composed of value

    i can create another array save the values in it and then use it in the function, however, this will results in lots of processing,

    i know the best way to do this would be to use array of values and just use one dimensional struct array instead, but i'm kiind of curious whether i can do it through double array struct

    Thanks a lot.
    Fadi
    Last edited by Fadibasma; 05-19-2009 at 08:11 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Fadibasma View Post
    so now i can't use

    function1 ( struct[x].value) // here i'm trying to pass the an array that is composed of value

    i can create another array save the values in it and then use it in the function, however, this will results in lots of processing,

    i know the best way to do this would be to use array of values and just use one dimensional struct array instead, but i'm kiind of curious whether i can do it through double array struct
    In answer to the first question, of course you can't. Why would you want to? You want an array, or you want a value. You are kind of saying "I cannot get a watermelon into my gas tank. What is the problem?" Show us the function.

    The phrase in red does not make logical sense. You can post actual code, try and use code tags if you do.
    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

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just pass the array of structs. You don't pass arrays by copy so you're not incurring a huge overhead (the only thing that gets copied is a pointer), and you can loop through the array inside to your heart's content.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    perhaps you meant
    Code:
    struct[x]->value; /* since struct[x] == &struct[x][0] */
    and don't use reserved keyword for variable names

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM