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?

0

1 Answer 1

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().

Sign up to request clarification or add additional context in comments.

12 Comments

Also, you need pass the length of the array.
Why size_t for the number (not the size) of the items?
@JohnColeman not sure what you mean. Would you prefer this to be int instead of size_t?
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.
int is a crappy idiom, size_t makes sense since that is the correct type for an array index. (I didn't vote)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.