Solved

`Passing an array of a structure as argument

Posted on 1998-06-17
15
193 Views
Last Modified: 2010-04-01
I would like know how to pass by reference an array of a structure as argument to a function and how I can use this array in the function.  Could somebody tell me how to do this please ?  Thanks.

My code looks like this:

struct TEST
{
  int a;
  int b;
  int c;
  bool d;
}

void main()
{
  TEST TstArray[10];
  F1(???????);
]

Void F1(?????)
{
   //Here , how could I use the array ?
}
0
Comment
Question by:tam031198
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 3

Accepted Solution

by:
Norbert earned 50 total points
ID: 1166047
Answer is coming
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1166048
void F1(TEST& *Array, int Size)
{
    for(int i=0;i<Size;i++)
    {
       Array[i].a=Something
.
    }
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166049
Small problem there.  Typo?  You want.

void F1(TEST *Array, int Size)
     {
         for(int i=0;i<Size;i++)
         {
            Array[i].a=Something
     .
         }
     }

or, I beleive this works, but I never use it, so I'm not sure.

void F1(TEST Array[], int Size)
     {
         for(int i=0;i<Size;i++)
         {
            Array[i].a=Something
     .
         }
     }





0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:alexo
ID: 1166050
Nietod's version is the correct one.

>> I beleive this works, but I never use it, so I'm not sure.
It works and also conveys more information to the reader.

And the call will be:

F1(TstArray, 10);
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166051
>>  conveys more information to the reader

What information? Just, the fact that it is an array and not an pointer to 1 item.  Or is there something else?  
0
 

Author Comment

by:tam031198
ID: 1166052
Here is my code:

#include <stdio.h>
struct TEST
{
      int a;
      int b;
      int c;
      bool d;
};

void main()
{
      void F1(TEST);  

        TEST TstArray[10];
      F1(TstArray);
        printf("Element 0 = %d\nElement 1 = %d\n",TstArray[0].a,TstArray[1].a);
}    

void F1(TEST *TstArray)
{
      TstArray[0].a = 1;
      TstArray[1].a = 10;
}

and I got this error while compiling:
error C2664: 'F1' : cannot convert parameter 1 from 'struct TEST [10]' to 'struct TEST'.

Could you tell me why please ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166053
You have declared two versions (overloads) of F1.  One was supposed to be a forward reference for the second, but it doesn't have the same parameters. It takes a signel TEST argument.  The other takes a pointer to an array of TEST  arguments.

Change the

void main()
     {
           void F1(TEST);  

       TEST TstArray[10];
           F1(TstArray);
       printf("Element 0 = %d\nElement 1 = %d\n",TstArray[0].a,TstArray[1].a);
     }    

to

void main()
     {
           void F1(TEST *);  

       TEST TstArray[10];
           F1(TstArray);
       printf("Element 0 = %d\nElement 1 = %d\n",TstArray[0].a,TstArray[1].a);
     }    

Note the "*" in the F1 declaration.
0
 

Author Comment

by:tam031198
ID: 1166054
Thank you very much Nietod, you are the King !!!!!!
0
 
LVL 11

Expert Comment

by:alexo
ID: 1166055
>> What information? Just, the fact that it is an array and not an pointer to 1 item.
Yep... King :-)
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1166056
Your function works but using your definition it does not know anything about the size of the array therefore it is better to pass the size also because the function then can handle arrays of different size

nietod: the '&' only was a typo - sometimes I should read what i have written before I hit the Submit button :-)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166057
Alex, I prefer "your highness."  
Nordbert, I figured it was a typo, the rest of the code suggested that.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1166058
Nordbert, any relation to Dilbert?
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1166059
Who is Dilbert ???
Nietod: Typos hapens to everyone :-)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166060
Mostly they happen to em.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1166061
>> Who is Dilbert ???
Man, what planet did you live on in the last 4 years?
www.unitedmedia.com/comics/dilbert
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question