17

I'm having trouble passing an array of structs to a function in C.

I've created the struct like this in main:

int main()
{
    struct Items
    {
        char code[10];
        char description[30];
        int stock;
    };

    struct Items MyItems[10];
}

I then access it like: MyItems[0].stock = 10; etc.

I want to pass it to a function like so:

 ReadFile(MyItems);

The function should read the array, and be able to edit it. Then I should be able to access the same array from other functions.

I've tried heaps of declarations but none of them work. e.g.

void ReadFile(struct Items[10])

I've had a look around for other questions, but the thing is they're all done different, with typedefs and asterisks. My teacher hasn't taught us pointers yet, so I'd like to do it with what I know.

Any ideas? :S

EDIT: Salvatore's answer is working after I fixed my prototype to:

void ReadFile(struct Items[10]);
Amir
  • 2,082
  • 4
  • 22
  • 28
  • It is "real code". I'm using it with Visual studio 2010 and it works. :S – Amir Nov 21 '11 at 00:30
  • 2
    "Visual Studio" and "it works" shall not be used in one sentence in polite conversation. You must say explicitly if you are talking about specific non-standard platforms; otherwise people will assume you are talking about the **standard** language. – Kerrek SB Nov 21 '11 at 00:32
  • 2
    Ok I fixed it. Hopefully that's real enough >. – Amir Nov 21 '11 at 00:33

9 Answers9

19
struct Items
{
    char code[10];
    char description[30];
    int stock;
};

void ReadFile(struct Items items[10])
{
    ...
}

void xxx()
{
    struct Items MyItems[10];
    ReadFile(MyItems);
}

This in my compiler works well. What compiler are you using? What error you got?

Remember to declare your struct before your functions or it will never work.

Salvatore Previti
  • 8,956
  • 31
  • 37
  • 1
    Thanks, that worked, after I fixed my prototype to void ReadFile(struct Items[10]); – Amir Nov 21 '11 at 01:34
7

Define struct Items outside of main. When passing an array to a function in C, you should also pass in the length of the array, since there's no way of the function knowing how many elements are in that array (unless it's guaranteed to be a fixed value).

As Salvatore mentioned, you also have to declare (not necessarily define) any structs, functions, etc. before you can use them. You'd usually have your structs and function prototypes in a header file in a larger project.

The below is a working modification of your example:

#include <stdio.h>

struct Items
{
    char code[10];
    char description[30];
    int stock;
};

void ReadFile(struct Items items[], size_t len)
{
    /* Do the reading... eg. */
    items[0].stock = 10;
}

int main(void)
{
    struct Items MyItems[10];

    ReadFile(MyItems, sizeof(MyItems) / sizeof(*MyItems));

    return 0;
}
AusCBloke
  • 18,014
  • 6
  • 40
  • 44
3

The function won't know that the type struct Items exists if you declare it only locally inside the main function body scope. So you should define the struct outside:

struct Item { /* ... */ };

void ReadFile(struct Items[]);   /* or "struct Item *", same difference */

int main(void)
{
  struct Item my_items[10];
  ReadFile(my_items);
}

This is dangerous of course since ReadFile has no idea how big the array is (arrays are always passed by decay-to-pointer). So you would typically add this information:

void ReadFile(struct Items * arr, size_t len);

ReadFile(my_items, 10);
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Your code has fixed the underlined errors, but it's still not building - I'm getting this: http://pastebin.com/HxPTcPmL – Amir Nov 21 '11 at 00:59
  • A) the errors have nothing to do with this part of the code, B) now we're confused about the language again -- suddenly it's C++?? – Kerrek SB Nov 21 '11 at 01:01
  • lol, sorry I got the wrong code, try again. That's from trying your code – Amir Nov 21 '11 at 01:01
  • Try what again? Pastebin doesn't let you change content; you get a new ID instead. – Kerrek SB Nov 21 '11 at 01:02
  • Here's my entire code I'm using in case you're curious: http://pastebin.com/YAXHdLtM – Amir Nov 21 '11 at 01:03
  • Put it all in one file first. Then start learning how to separate compilation units. The definition of the struct must be known to the `ReadFile` function that uses it... baby steps, eh :-) – Kerrek SB Nov 21 '11 at 01:06
  • Argh. I put it in one file and it's still not building. Guess the problem might be with Visual studio; thanks for trying >. – Amir Nov 21 '11 at 01:15
  • On second thought: It was all my stupidity. I was meant to put the struct in the header file... And when I put it all in 1 file, I wasn't declaring the function prototype correctly. Sorry for the bother!! It's working now with Salvatore's answer (and probably yours as well) :) – Amir Nov 21 '11 at 01:42
1

Instead of your declaration, declare in that way:

typedef struct {
        char code[10];
        char description[30];
        int stock;
}Items;

and the function like that:

void ReadFile(Items *items);

With typedef you define a new type, so you don't need to use word "struct" each time.

Nikita
  • 1,811
  • 1
  • 20
  • 41
0

Why dont you use pass the pointer to the array to the methods that need it?

If you want the same struct array then you should use pointer to the array and not pass as array as that will create a copy.

void ReadFile(struct Items * items);

and where you call it

struct Items myItems[10];
ReadFile(myItems);

Need to be careful with pointers...

Sid Malani
  • 2,078
  • 1
  • 13
  • 13
0

You pretty much have to use pointers for this. You function would look like this:

void ReadFile(Items * myItems, int numberOfItems) {
}
Andrew Kolesnikov
  • 1,920
  • 1
  • 14
  • 20
0

You need to use pointer to array, after that its easy to access its members

void ReadFile(Items * items);

should work.

nikola-miljkovic
  • 652
  • 4
  • 14
0

Well, when you pass a structure like you did, it actually creates a local copy of it in the function. So it will have no effect on your original structure, no matter how you modify it in ReadFile.

I am not sure about a different approach and this might not answer your question, but I recommend you try pointers. You'll definitely be using them quite a lot in C/C++. And they can be really powerful once you master them

Kaisar
  • 35
  • 2
  • 8
  • Not true, you can try that out, C passes array always by pointer, you can modify it in ReadFile, copy is not involved here. – Salvatore Previti Nov 21 '11 at 00:44
  • @SalvatorePreviti You are absolutely right! For some odd reason I understood that he wanted to use the structure in his method, not the array. -_- – Kaisar Nov 25 '11 at 09:00
0

Have you tried to declare you function like this:

void ReadFile(struct Items[])

Might be helpful: http://www.daniweb.com/software-development/cpp/threads/105699

max
  • 1,579
  • 1
  • 19
  • 34