Array of struct pass to function 
Author Message
 Array of struct pass to function

I have a structure:

struct record {

 char filename[50];
        int facility;
        int level;
        int fd;

     };

then In main I do:

struct record files[50];

can someone help me, how do I pass the complete structure into a function ?
Been trying four hours not with no joy. I have tried:

int test(record *files){

return 0;

Quote:
}

etc, but I get all sorts of errors.
any help would be appreciated.


Sat, 29 Oct 2005 21:56:50 GMT  
 Array of struct pass to function

Quote:
> I have a structure:
> struct record {
>  char filename[50];
>         int facility;
>         int level;
>         int fd;
>      };
> then In main I do:
> struct record files[50];
> can someone help me, how do I pass the complete structure into a function ?
> Been trying four hours not with no joy. I have tried:
> int test(record *files){
> return 0;
> }
> etc, but I get all sorts of errors.
> any help would be appreciated.

Try:
int test(struct record *files) {
  return 0;
Quote:
}

and then in main() you call something like this:
test(files);

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"'It can be easily shown that' means 'I saw a proof of this once (which I didn't
understand) which I can no longer remember'."
   - A maths teacher



Sat, 29 Oct 2005 22:03:52 GMT  
 Array of struct pass to function
Try
 int test (struct record *files) {
Quote:

> I have a structure:

> struct record {

>  char filename[50];
>         int facility;
>         int level;
>         int fd;

>      };

> then In main I do:

> struct record files[50];

> can someone help me, how do I pass the complete structure into a function ?
> Been trying four hours not with no joy. I have tried:

> int test(record *files){

> return 0;
> }

> etc, but I get all sorts of errors.
> any help would be appreciated.



Sat, 29 Oct 2005 22:16:21 GMT  
 Array of struct pass to function

Quote:

> I have a structure:

> struct record {

>  char filename[50];
>         int facility;
>         int level;
>         int fd;

>      };

> then In main I do:

> struct record files[50];

> can someone help me, how do I pass the complete structure into a function ?

_One_ complete structure? Or the complete array of structures? In the
first case, just pass files[n]. In the second, that cannot be done. You
cannot directly pass arrays to functions; you can only pass pointers
(though you could trick the compiler by passing the array inside yet
another struct, but that quickly gets more complicated than it's worth).

Quote:
> Been trying four hours not with no joy. I have tried:

> int test(record *files){

C is not C++; a declaration of struct record does not also declare a
type called record. Try this:

  int test(struct record *files){

Richard



Sat, 29 Oct 2005 21:57:46 GMT  
 Array of struct pass to function
Thank you for your reply.

Yes if I could access the complete array using a pointer then it would do
the job; also I have tried the other suggestions with no joy.


Quote:
> Try
>  int test (struct record *files) {


> > I have a structure:

> > struct record {

> >  char filename[50];
> >         int facility;
> >         int level;
> >         int fd;

> >      };

> > then In main I do:

> > struct record files[50];

> > can someone help me, how do I pass the complete structure into a
function ?
> > Been trying four hours not with no joy. I have tried:

> > int test(record *files){

> > return 0;
> > }

> > etc, but I get all sorts of errors.
> > any help would be appreciated.



Sat, 29 Oct 2005 22:20:00 GMT  
 Array of struct pass to function

Quote:
> Thank you for your reply.
> Yes if I could access the complete array using a pointer then it would do
> the job; also I have tried the other suggestions with no joy.

What do you mean "if I could access the complete array using a pointer"?
What's stopping you?
For the record, files[0] through files[49] mean the exact same thing
in test() as they do in main(). The only thing you can't do in test is
query the files variable to see how large the array is. You must either
know it in advance or pass in a size parameter or something.

Note that everyone who replied gave you the exact same solution. So if
you have tried one of them, you have effectively tried them all.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"I said 'play as you've never played before', not 'play as IF you've never
played before'!"
   - Andy Capp



Sun, 30 Oct 2005 01:30:13 GMT  
 Array of struct pass to function

Quote:

> I have a structure:

> struct record {

>  char filename[50];
>         int facility;
>         int level;
>         int fd;

>      };

> then In main I do:

> struct record files[50];

> can someone help me, how do I pass the complete structure into a function ?
> Been trying four hours not with no joy. I have tried:

> int test(record *files){

> return 0;
> }

> etc, but I get all sorts of errors.
> any help would be appreciated.

What do you mean by "all sorts of errors"? Please be specific as to
whether you are experiencing compiler errors or runtime errors, and
fully describe them.

Also, please post a complete, minimal program that demonstrates your
problem.

Brian Rodenborn



Sun, 30 Oct 2005 01:49:11 GMT  
 Array of struct pass to function

Quote:

> Thank you for your reply.

> Yes if I could access the complete array using a pointer then it would do
> the job; also I have tried the other suggestions with no joy.

void fill(struct record *rec, int N);
void test(struct record *rec, int N);

int main(void)
{
  struct record files[50];

  fill(files, 50);
  test(files, 50);

Quote:
}

void fill(struct record *rec, int N)
{
  int i;

  for(i=0;i<N;i++)
    sprintf(rec[i].filename, "test%d", i+1);

Quote:
}

void test(struct record *rec, int N)
{
  int i;

  for(i=0;i<N;i++)
    printf("%s\n", rec[i].filename);

Quote:
}



Sun, 30 Oct 2005 02:25:58 GMT  
 Array of struct pass to function

Quote:

> I have a structure:

> struct record {

>  char filename[50];
>         int facility;
>         int level;
>         int fd;

>      };

> then In main I do:

> struct record files[50];

> can someone help me, how do I pass the complete structure into a function ?
> Been trying four hours not with no joy. I have tried:

> int test(record *files){

> return 0;
> }

> etc, but I get all sorts of errors.
> any help would be appreciated.

Use 'struct record'. If you want to refer to it as just 'record' you need
to do:
typedef struct record record;


Sun, 30 Oct 2005 02:57:42 GMT  
 Array of struct pass to function
Thankyou all for your answers, Malcolm solved it.


Sun, 30 Oct 2005 16:41:53 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. array of Struct passing into a function

2. Passing a struct with an array to C++ function

3. pass a pointer to array of structs to functions

4. How to pass struct array to called function

5. How to pass a struct to an unsigned char array

6. Passing array of struct

7. passing adress of second element in array of structs

8. How to pass a struct to an unsigned char array

9. Passing an array of structs

10. Passing an array of structs

11. passing arrays and returning structs

12. Passing structs and arrays --- marshaling and IDL issues...

 

 
Powered by phpBB® Forum Software