Struct With New.... Dynamic

how I can access struct array in any other function .. like in this program i want to access in GetData function.. Help me..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #include<iostream.h>


/* structure*/
struct Reservation         //defining the structure 
{
	char pName[20];
	char pAdress[30];
	int flight_date;
	char destination[20];
	int flight_no;
	int seat_no;
};

/*Function*/
void GetData(Reservation rs, int size_of_reservation);

/*Main Function*/

void main ()
{
	int size_of_reservation;
	cout<<"Enter the number of reservation:";
	cin>>size_of_reservation;
	Reservation *rs = new Reservation[size_of_reservation];
	
	GetData(*rs,size_of_reservation);

}

/* Function Will Get User Data */

void GetData(Reservation *rs, int size_of_reservation)
{
	for(int i=0; i<=size_of_reservation; i++)
	{
		cout<<"Please Enter Passenger Name. ";
		cin>>rs[i].pName;

	}
}

The function declaration at line 16 does not match the corresponding function definition at line 33.

The function call at line 27 should read:
 
    GetData(rs,size_of_reservation);

Before the end of main(), you should release the memory allocated by the previous new[]:
 
    delete [] rs;

Other comments, in standard C++ you should use #include <iostream> without the 'h', and main should be declared as int main() (definitely not void).
how I can access struct array in any other function

Pass it as an argument just like you did to GetData.

I'm not sure I understand your question. You have already done this.
Can you clarify your question? What is it you're trying to do, or don't understand?

If you want *rs to be available to any function, you can make *rs global, but that is a practice that should be avoided.



Abstraction Anon ..

I know how to pass array as argument but I'm confuse about struct :(

both have the same procedure???

and you'r right, I want """ Reservation rs[] """ to all functions.. but dont want to make it global..


sorry for my english.....
Usually, the simplest way to get the definition and prototype declaration to match is to copy&paste.
In this case copy line 33 and paste it just above line 16:

15
16
17
/*Function*/
void GetData(Reservation *rs, int size_of_reservation)   // was line 33
void GetData(Reservation rs, int size_of_reservation);   // old line 16 


See the difference now?

Next add a semicolon to the end of the new line 16 and delete the old one.

One more thing. Change <= to < in the for loop at line 35.
Last edited on
If I understand your question correctly, here's an example of passing a single struct occurrance by reference:
1
2
3
4
5
6
7
8
9
void AddReservation (Reservation &r)  // refers to a single reservation entry
{   cout<< "Please Enter Passenger Name. ";
    cin >> r.pName;	
}

void GetData(Reservation *rs, int size_of_reservation)
{   for(int i=0; i<=size_of_reservation; i++)
         AddReservation (rs[i]);  // Pass a single reservation entry
}

Thank you all of you..
Topic archived. No new replies allowed.