Solved

`Passing an array of a structure as argument

Posted on 1998-06-17
15
192 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

777 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