Solved

`Passing an array of a structure as argument

Posted on 1998-06-17
15
190 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now