Pointers, structs, and functions, oh my!

This is a discussion on Pointers, structs, and functions, oh my! within the C++ Programming forums, part of the General Programming Boards category; Hey, here's a real quick question: How do I pass an array of structs as a pointer to a void ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Pointers, structs, and functions, oh my!

    Hey, here's a real quick question:

    How do I pass an array of structs as a pointer to a void function?

    I'm in the middle of learning pointers and I'm a complete n00b at pointers. Maybe something like:

    Code:
    struct FOO
    {
         int foo;
    };
    
    void I_eat_foo_too(FOO *foo[]);
    
    int main()
    {
    
         FOO foo[5];
    
         I_eat_foo_too(&foo);         //Hmm....
      
    }
    
    void I_eat_foo_too(FOO *foo[])
    {
         foo[0]->foo = 123;
    }
    Last edited by funkydude9; 07-29-2003 at 03:12 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  2. #2
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    First, I absolutely love the title of your post

    Anyways, here how I would do it. It's always safest to pass the length of the array, too, so that in the function you can check that the index is in bounds.

    Code:
    struct FOO
    {
         int foo;
    };
    
    void I_eat_foo_too(FOO *foo[]);
    
    int main()
    {
    
         FOO foo[5];
    
         I_eat_foo_too(foo, 5);  
         // The name of an array is just a pointer to the first 
         // element of the array! 
      
    }
    
    void I_eat_foo_too(FOO *foo, int fooLength)
    {
         foo[0]->foo = 123;
    }
    My programs don't have bugs, they just develop random features.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    void I_eat_foo_too(FOO (*foo)[5]);
    ...
    
    FOO foo[5];
    I_eat_foo_too(&foo);

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    You and codegirl are both actually declaring the parameter of the function as an array of FOO pointers. Your question does not require that the parameter to the function be an array of pointers, but rather you require a single pointer to an array of FOO structures, so you can use either of the following methods to declare your function:

    void I_eat_foo_too(FOO my_array[]);

    or

    void I_eat_foo_too(FOO* parray);

    and you would call the function like this for both declarations:

    FOO foo[5];

    I_eat_foo_too(foo);

    That is a fairly obvious way to call the function for the first declaration, but it also works for the second declaration since an array name is actually a pointer.

    In addition, you will not use the -> operator to access the data members of the elements of the array since the elements of the array aren't pointers, rather the elements of the array are FOO structs, so you need to use the . operator to access the data members:

    my_array[0].foo = 123; //for the first declaration

    or

    parray[0].foo = 123;//for the second declaration

    I hope that helps.
    Last edited by 7stud; 07-29-2003 at 04:22 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Oh cool. Hey codegirl, I think you ment to make the function prototype like this:
    Code:
    void I_eat_foo_too(FOO *foo);
    instead of
    Code:
    void I_eat_foo_too(FOO *foo[]);
    Right?
    I'm also kind of unclear on why you dont use -> to acess member variables, since your passing it as a pointer. Seems strange.
    Thanks guys for the help.
    Last edited by funkydude9; 07-29-2003 at 08:41 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    I believe the '->' is used like this:

    instead of typing "(*ptr).a;" you can type this "ptr->a;"

    Typically used in linked lists.

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    See my Signature
    Code:
     |
     |
    \ /
     "
    Click Pointers And Memory

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "I'm also kind of unclear on why you dont use -> to acess member variables"

    Its simple: you use the -> operator on pointers to objects, and the . operator on objects. So, you have to ask yourself whether the elements of your array are objects or pointers to objects. How you pass the array to the function is irrelevant.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    #include <iostream>
    using namespace std;
    
    struct FOO
    {
         int foo;
    };
    
    void I_eat_foo_too(FOO *foo);
    
    int main()
    {
         FOO foo[5];
         I_eat_foo_too(foo);  
         system("PAUSE");
         
    }
    
    void I_eat_foo_too(FOO *f)
    {
       f[0].foo = 12;
       cout << f[0].foo << endl;
       (*f).foo = 13;
       cout << f[0].foo << endl;
       f->foo = 14;
       cout << f[0].foo << endl;
    }
    This seems to work fine on my compiler, I used three different ways of assigning values to the struct. Hope this is correct

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 01:35 AM
  2. Array of Pointers to Functions in Structs...
    By yaya in forum C++ Programming
    Replies: 10
    Last Post: 12-21-2008, 06:14 PM
  3. passing pointers at structs thru functions
    By nunnu in forum C Programming
    Replies: 2
    Last Post: 05-12-2004, 10:16 AM
  4. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

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