Using multidimensional dynamic arrays as argument in function

I am trying to use a multidimensional dynamic array as an argument in a function. I want to display the seating chart of an airplane. The chart is to have a number of rows specified by the user. Each row has 4 columns. All of the seats in the first column are labeled, A; the second column, B; the third, C; and the fourth, D. For example:

1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D

When I call the function on line 35 output_seat_chart(seat_chart, num_of_rows);I get a red error line under the argument, seat_chart. When I try to run the program I get the error message, Error (active) argument of type "CharArrayPtr *" is incompatible with parameter of type "char (*)[4]"
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
44
45
46
47
48
49

#include <iostream>
using namespace std;

void output_seat_chart(char seat_chart[][4], int num_of_rows);

typedef char* CharArrayPtr;
//Defines type name CharArrayPtr that can be used to declare
//pointers to dynamic variables of type char

int main()
{
	int num_of_rows;

	cout << "Enter the number of rows on the plane: ";
	cin >> num_of_rows;

	CharArrayPtr *seat_chart = new CharArrayPtr[num_of_rows];
	//Creates pointer (seat_chart) to an array of num_of_rows elements.

	for (int i = 0; i < num_of_rows; i++)
		seat_chart[i] = new char[4];
	//Creates a 4 element array for each seat_chart[0]
	//through seat_chart[num_of_rows - 1].

	for (int i = 0; i < num_of_rows; i++)
		seat_chart[i][0] = 'A';
	for (int i = 0; i < num_of_rows; i++)
		seat_chart[i][1] = 'B';
	for (int i = 0; i < num_of_rows; i++)
		seat_chart[i][2] = 'C';
	for (int i = 0; i < num_of_rows; i++)
		seat_chart[i][3] = 'D';

	output_seat_chart(seat_chart, num_of_rows);
	
	system("pause");
	return 0;
}
void output_seat_chart(char seat_chart[][4], int num_of_rows)
{
	for (int i = 0; i < num_of_rows; i++)
	{
		cout << i + 1 << ' ' 
			<< seat_chart[i][0] << ' ' << seat_chart[i][1]	<< ' '
			<< seat_chart[i][2] << ' ' << seat_chart[i][3] << endl;
	}
	cout << endl << endl;
}
Last edited on
output_seat_chart(...) expect a fixed size array. You provide a variable array. The types are not compatible.

Change to:
void output_seat_chart(CharArrayPtr *seat_chart, int num_of_rows)
Thank you! That fixed the problem. The only other change I had to make was to put
1
2
3
typedef char* CharArrayPtr;
//Defines type name CharArrayPtr that can be used to declare
//pointers to dynamic variables of type char 
above the declaration
void output_seat_chart(CharArrayPtr *seat_chart, int num_of_rows);
Last edited on
Topic archived. No new replies allowed.