Solved

`Passing an array of a structure as argument

Posted on 1998-06-17
15
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

738 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