Thread: Passing a pointer to an array of structs in C

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    8

    Question Passing a pointer to an array of structs in C

    I hope the title did not discourage a lot of people.

    So, what I am trying to do is to pass to a function the address of an array of structs, so I can later modify the items within the struct, within the array

    Code:
    typedef struct {  //A struct of name auction
    int bidder;
    float bid;
    } auction; void myFunction (auction * auctionItem[]){
    (*aucItem[x]).bid = y;
    (*aucItem[x]).bidder = z;
    } int main(){
    auction theItems[10]; myFunction(theItems);
    }
    Where x, y, and z can be any number.
    When I try to run my code the IDE (I'm using Code::Blocks 12.11) does not give me any errors, but it does give me a warning:

    warning: passing argument 3 of '<function name>' from incompatible pointer type [enabled by default]
    and the note:

    note: expected 'struct <struct name> **' but argument is of type 'struct <struct name> *'
    Also, when I run the program, it will crash and return garbage.

    I may be going further than my skill level, but I do not want to let this idea go yet. So thank you very much for your help.
    Last edited by OSamo; 11-20-2013 at 08:41 PM. Reason: Added details

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you pass an array into a function, the objects inside the array are modifiable already without any special work on your part.

    Declaring "auction * auctionItem[]" means you have an array-of-pointers, not a pointer-to-array. However, as mentioned above, you don't want a pointer-to-array, so that's okay.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    If I use
    Code:
    void myFunction (auction * auctionItem[])
    instead, and pass to it the same values, it gives me the error "error: conflicting types for '<function name>'"

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Let me try again.

    Don't type *. It may require someone physically holding you back from the keyboard, but you're going to have to resist the urge to type *******************. No *.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    My bad, I copied the old code without removing the *.
    The errors they give me after I remove * are those, however.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by OSamo View Post
    My bad, I copied the old code without removing the *.
    The errors they give me after I remove * are those, however.
    No it doesn't. It might give you "left side of operand is not pointer or array" if you forgot to take the stars off the aucItem[x] lines, but you wouldn't get that error. (Unless you have a prototype up above it that you forgot to take the star out of.)

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    Ah, you were right about the prototype. I fixed that, and in the function I did not deference when I used the struct. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  2. Passing an array of structs
    By SleepingUdon08 in forum C Programming
    Replies: 2
    Last Post: 04-03-2009, 11:23 PM
  3. Error passing an array of structs
    By Catalyst8487 in forum C++ Programming
    Replies: 10
    Last Post: 12-15-2008, 03:38 PM
  4. passing an array of structs
    By izuael in forum C++ Programming
    Replies: 14
    Last Post: 11-26-2006, 03:00 PM
  5. Passing an array of structs to a function
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-03-2001, 12:02 PM

Tags for this Thread