• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2009
  • Last Modified:

Passing An Array Of Structures Between Functions

Hello Experts

I have a structure which I pass between functions successfully.
I want to change that so rather than pass a single structure, I pass an array of structures.

How do I do that?

Thanks
0
stummj
Asked:
stummj
  • 6
  • 5
  • 2
1 Solution
 
ikeworkCommented:
hi stummj,


#include <iostream>

using namespace std;

struct Test
{
    int value;
};

void dump_array( Test *pTest, int count )
{
      for( int i = 0; i < count; ++i )
      {
            cout << pTest[ i ].value << endl;
      }
}

int main( int argc, char** argv )
{
      Test a[ 5 ];

      for( int i = 0; i < 5; ++i )
      {
            a[ i ].value = i;
      }

      dump_array( a, 5 );

      return 0;
}


hope it helps :)
ike
0
 
ikeworkCommented:
alternatively:

void dump_array( Test  pTest[], int count )


has the same effect ...
0
 
stummjAuthor Commented:
Hi ike!

Thanks for this. Is it passing each element one at a time? I really wanted to pass the array as a single entity.

i.e. something like....

typedef struct product_
{
  char name[20];
  int ref;
} product_t;

product_t  productarray[5]

addAllProducts(*productarray)

addAllProducts(product_t*  productarray)    /* I think this is where Im having trouble. the type isnt a product its an array of products? */
{
  char test[20];
   strcpy (test,productarray[1].name);
}

Do you get my drift?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sunnycoderCommented:
When you pass an array, it is received in the called function as a pointer to the type of the array.

typedef struct product_
{
  char name[20];
  int ref;
} product_t;

product_t  productarray[5]; //declaration of array

//this is how you call it ... just pass the name of the array
addAllProducts(productarray);

addAllProducts(product_t*  productarray)    /* I think this is where Im having trouble. the type isnt a product its an array of products? */
//this is perfectly fine ... the array would be received as a pointer to type product_t ...
//Note that this applies to single dimension arrays only, a 2D array would not be received as a pointer to pointer
{
  char test[20];
  for (int i=0; t < 5; i++ )
           printf("%s\n", productarray[i]);
}
0
 
stummjAuthor Commented:
Thanks sunnycoder. Thats looking good I think.

Looking at the difinition of product, we see that my (simple) structure has 2 elements, name and ref.

How would I populate them in the array, and retrieve the data in the receiving function?

Assuming I have an instance of the structure - lets call it productinstance correctly popuated with the following data:

productinstance[0].name = "name0"
productinstance[0].ref = 0

how do I get that structure to be the first element in the arrray of structures (productarray)

Is it as simple as productarray[0] = productinstance[0]?
0
 
stummjAuthor Commented:
I made a mistake there - its getting late to be working here in the UK!

I meant...

Assuming I have an instance of the structure - lets call it productinstance correctly popuated with the following data:

productinstance.name = "name0"
productinstance.ref = 0

how do I get that structure to be the first element in the arrray of structures (productarray)

Is it as simple as productarray[0] = productinstance

and accessing the elements in the receiving function something like...


strcpy(element,productarray[0].name) etc to give me "name0" .. if you see what I mean..

0
 
sunnycoderCommented:
>productinstance.name = "name0"
>productinstance.ref = 0

>how do I get that structure to be the first element in the arrray of structures (productarray)

>Is it as simple as productarray[0] = productinstance

There are a few ways to do that ... If you wish to avoid copying and single copy is more desirable, then use an array of pointers to struct rather than an array of structs ... You can then simply assign the pointer to this struct to the first element of the array

product_t * productarray[5]; //declaration of array

productarray[0] = &productinstance;

To access respective elements use
productarray[0]->name and productarray[0]->ref

Another possibility is to use the same data structures that you have and copy each element ...
strcpy(productarray[0].name, productinstance.name);
productarray[0].ref = productinstance.ref;

Simple assignment of whole structs will not work. You have to resort to element by element copy

Yet another possibility is to use a memcpy but that is not recommended since there can be issues with structure padding ...

Cheers!
sunnycoder
0
 
stummjAuthor Commented:
I think we are definately making great progress here sunnycoder so thank you. Im learning loads through doing this!

We still have the initial problem though, and that is how do we pass the array between functions.
From my earlier post....

At the moment Im doing this:
---------------------------------------------------------------------------------
addAllProducts(productarray);

addAllProducts(product_t*  productarray)    /* I think this is where Im having trouble. the type isnt a product its an array of products? */
//this is perfectly fine ... the array would be received as a pointer to type product_t ...
//Note that this applies to single dimension arrays only, a 2D array would not be received as a pointer to pointer
{
  char test[20];
  for (int i=0; t < 5; i++ )
           printf("%s\n", productarray[i]);
}
----------------------------------------------------------------------------------------------
Doesnt  the (product_t *productarray) imply that Im passing through a single instance of a product structure, rather than the array (or rather pointer to the first element in the array) that I think I should be passing? Should the receiver be something like addAllProducts(&productarray)?

Julian
0
 
sunnycoderCommented:
>Doesnt  the (product_t *productarray) imply that Im passing through a single instance of a product structure, rather than
>the array (or rather pointer to the first element in the array)
No it implies that you are passing a pointer to struct ... This could as well be pointer to first element of an array of structs.
It is really upto you to code your function to access just the struct pointed to by this pointer or use it like an array variable.
0
 
stummjAuthor Commented:
Hmm. Something isnt right.

When I try this:

Function1
-----------
productarray[0] = &pProductLine;
if (addTmpProduct(productarray) != 0
{
    error()
}


Function 2
------------
int addTmpProduct (productline_t *productarray)
{
   strcpy(custref,productarray[0]->custRef);
   RETURN 0
}

I get this compile error
"function2.c", line 3.31: 1506-122 (S) Expecting pointer to struct or union.

What am I doing wrong sunnycoder?
0
 
sunnycoderCommented:
productline_t *productarray

productarray is pointer ... so ...

productarray[0] is an element .. i.e. a struct and not pointer to struct ...

Use . and not ->
0
 
stummjAuthor Commented:
I think thats cracked it sunnycoder thank you so much. There are a alot of new things Ive learnt from this.
Can you recommend a book that explains the language in a straightforward way like you do?
Please dont say Kernigan and Ritchie. I have that book but its nowhere near as easy to understand as how you explain it!

(The points are yours - I'll leave this open just so you can tell me if you know a good book!)
0
 
sunnycoderCommented:
Thanks so much for the compliments :)

Honestly, I have not read much books on programming in C language ... So I am not in a position to recommend many options. If it is available in your part of the world, you can try "Let Us C" by Yeshwant Kanetkar .. it is quite simple but beware it has quite a few errors too.

Some book reviews
http://www.cprogramming.com/books/c.html
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now