0

I want to know how I can send my array of structure to a function.

typedef struct {
    char fname[20];
    char lname[20];
    int cnumber[12];
} contact;

contact record[40];

int main()
{
    // I have all the data in the record array as I am reading it from the
    // file and want to pass the record array to the function PRINT and access it.
    print();
}

How can it be send in the function and print all the values using function call?

Stargateur
  • 24,473
  • 8
  • 65
  • 91

1 Answers1

1

You can send your array of structures to a function like this:

void print(contact record[], int n) {

Then print the contents in this function and send it back to main() as:

print(record, n);

Note: the length of the array, n, should be kept track of somewhere in your program, then passed to print().

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • 1
    Also, you need pass the length of the array. – army007 Feb 08 '17 at 04:29
  • 1
    Why `size_t` for the number (not the size) of the items? – John Coleman Feb 08 '17 at 04:32
  • @JohnColeman not sure what you mean. Would you prefer this to be `int` instead of `size_t`? – RoadRunner Feb 08 '17 at 04:35
  • 1
    It is a pedantic point since in virtually all implementations `size_t` is likely large enough to hold the number of elements in virtually any array, but `int` is a bit more idiomatic. More importantly, it is a little clearer. – John Coleman Feb 08 '17 at 04:38
  • Hey so passing record will pass a pointer to array right?can somebody clear – minigeek Feb 08 '17 at 05:00
  • Can I ask whoever down voted this to leave a comment as to what can be improved here. – RoadRunner Feb 08 '17 at 05:51
  • @minigeek It just passes an array. Pointers and Arrays are different in C, Arrays decay to pointers. Specifically in this case, you are just passing an array. But you can instead pass pointers like `contact *record` instead if you wish. – RoadRunner Feb 08 '17 at 05:56
  • @RoadRunner yesterday one of the fellow on so (40k+repu) told me that there is no such thing as passing whole array into function whether you write func(char *arr) or func(char arr[ ]).both of them basically do the same thing.uptil now i assumed when we pass array whole array gets copied into local array but in real it just passes pointer to initial address. I am confused :/ – minigeek Feb 08 '17 at 06:11
  • @minigeek [read this](http://stackoverflow.com/questions/6567742/passing-an-array-as-an-argument-to-a-function-in-c). This seems to explain this quite well. When I said "*passing an array*" , I meant it will pass the address of the first element in the array. Whereas a pointer will past a pointer to the first element in the array. That's why their is no difference. I tend to not over complicate things like this, as it will only lead to alot of confusion. – RoadRunner Feb 08 '17 at 06:38
  • 1
    `int` is a crappy idiom, `size_t` makes sense since that is the correct type for an array index. (I didn't vote) – M.M Feb 08 '17 at 06:42
  • @M.M That's what I thought. Others think differently though. – RoadRunner Feb 08 '17 at 07:53
  • The C library uses `size_t` in nearly all functions where a _size_ is needed. `*alloc()`, `snprintf()`, `strlen()`,... (`fgets(char *s, int, FILE *stream)` being a counter example. Portable code uses `size_t`. – chux - Reinstate Monica Feb 08 '17 at 20:19