Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

`Passing an array of a structure as argument

Posted on 1998-06-17
15
Medium Priority
?
197 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

927 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