Passing struct[]

Hello.

I'm having some difficulty working with a structure. From my understanding, C++ passes structs by value to functions.

I would like to pass an struct array by reference, so I can make changes to the data inside of any of the elements of any member of the struct array.

I tried this:

[code]void my_function(some_struct *array[], int some_int);[/code]
but when I try to actually call the function, like this:

[code]my_function(&array, some_int);[/code]
I receive the error:

[code]error: cannot convert 'some_struct (*)[500]' to 'some_struct**'[/code]
If I do not try to pass as a pointer, I can not make any changes to the data that lasts beyond leaving the my_function function.

I would prefer not to make the structure array a global element, but I can't think of any other solution.

Any advice or suggestions? Thanks in advance.

Comments

  • : Hello.
    :
    : I'm having some difficulty working with a structure. From my understanding, C++ passes structs by value to functions.
    :
    : I would like to pass an struct array by reference, so I can make changes to the data inside of any of the elements of any member of the struct array.
    :
    : I tried this:
    :
    : [code]void my_function(some_struct *array[], int some_int);[/code]
    : but when I try to actually call the function, like this:
    :
    : [code]my_function(&array, some_int);[/code]
    : I receive the error:
    :
    : [code]error: cannot convert 'some_struct (*)[500]' to 'some_struct**'[/code]
    : If I do not try to pass as a pointer, I can not make any changes to the data that lasts beyond leaving the my_function function.
    :
    : I would prefer not to make the structure array a global element, but I can't think of any other solution.
    :
    : Any advice or suggestions? Thanks in advance.
    :


    are you attempting to pass a pointer to a single struct object or to an array of objects? The function prototype takes a 2-dimensional array. Like this:
    [code]
    struct some_struct
    {
    int a,b,c;
    };

    void my_function(some_struct *array[], int some_int);

    int main()
    {

    // an array of 20 pointers to structures
    some_struct *array[20];
    my_function(array,20);
    }
    [/code]
  • [b][red]This message was edited by Delphi at 2006-8-28 18:30:27[/red][/b][hr]
    Thanks for the quick reply!

    I was trying to pass an array of the struct:

    [code]my_struct some_struct[500];[/code]

    And I see from your reply my mistake. I was trying to use '&' to pass the array by reference, but I forgot that wouldn't work.

    Simply had to make the structure array a pointer to begin with and all the problems were solved.

    Well, aside from having to work with pointers. Nasty things ;-)


  • Well, I thought that it had fixed up the issues. Compiled successfully, but it kills as soon as it opens.

    Pointers definitely aren't my strong point, but here's what's giving me trouble:

    [code]my_struct *some_struct[500];

    some_struct[0]->has_data_ready = true;[/code]
    It kills at the second line, and I can't remember for the life of my how to access that data, but I know what I'm doing isn't the correct way, since it kills.

    Quick check on google didn't turn up anything and I don't have access to my text books right now, or I'd be flipping through those.

    Any chance anyone could offer some guidance?
  • : Well, I thought that it had fixed up the issues. Compiled successfully, but it kills as soon as it opens.
    :
    : Pointers definitely aren't my strong point, but here's what's giving me trouble:
    :
    : [code]my_struct *some_struct[500];
    :
    : some_struct[0]->has_data_ready = true;[/code]
    : It kills at the second line, and I can't remember for the life of my how to access that data, but I know what I'm doing isn't the correct way, since it kills.
    :
    : Quick check on google didn't turn up anything and I don't have access to my text books right now, or I'd be flipping through those.
    :
    : Any chance anyone could offer some guidance?
    :


    While you may pass structs by value (bad programming in 99% of the cases), you can't pass arrays by value in C/C++.

    This is the most common way to do it:

    [code]
    typedef struct
    {
    ...
    }MyStruct;


    void func(MyStruct* myStruct);

    int main()
    {
    MyStruct myStruct[x];

    func(myStruct);


    return 0;
    }

    void func(MyStruct* myStruct)
    {
    myStruct[0] = ...
    }

    [/code]


    Or if you want to limit the function to only accept an array of a given size:

    void func(MyStruct myStruct[100]);

    [b]Note that this is not an array declaration! The array is still passed by reference![/b]
  • [b][red]This message was edited by stober at 2006-8-29 0:20:52[/red][/b][hr]
    : Well, I thought that it had fixed up the issues. Compiled successfully, but it kills as soon as it opens.
    :
    : Pointers definitely aren't my strong point, but here's what's giving me trouble:
    :
    : [code]my_struct *some_struct[500];
    :
    : some_struct[0]->has_data_ready = true;[/code]
    : It kills at the second line, and I can't remember for the life of my how to access that data, but I know what I'm doing isn't the correct way, since it kills.
    :
    : Quick check on google didn't turn up anything and I don't have access to my text books right now, or I'd be flipping through those.
    :
    : Any chance anyone could offer some guidance?
    :


    Note that [b]my_struct *some_struct[500]; [/b] is an array of pointers which must first be allocated before they can be used. If you are writing your own functions then you should change it to remove the pointers and just pass the array of structures itself -- see Lundin's example. But if you cannot change it for some reason, then you will have to allocate memory for all those pointers
    [code]
    my_struct *some_struct[500];
    for(int i = 0; i < 500; i++O
    some_struct[i] = new my_struct;
    [/code]


  • Aha. Thank you, Lundin.

    Your example was more to what I was trying to accomplish; passing single structures or passing an array of structures.

    When I had tried with stober's suggestion, it appears I was passing **some_struct to my functions, which I didn't want.

    Though, thanks to stober as well, because he's offered help on many of my other issues, as well as on this one :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories