Reputation: 137
int main() {
int casee;
int m, n;
int k;
char* a;
char ch;
cin >> casee;
cin >> m;
cin >> n;
a = new char[m];
for (int i = 0; i < m; i++) {
a[i] = new char[n]; // # 20
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> ch;
a[i][j] = ch; // # 28
}
}
cout << a[2][3] << endl; // # 32
return 0;
}
In 28, 30, 32 lines, i have some error like this..
proj1.cpp: In function ‘int main()’:
proj1.cpp:20:18: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
proj1.cpp:28:10: error: invalid types ‘char[int]’ for array subscript
proj1.cpp:32:15: error: invalid types ‘char[int]’ for array subscript
I want to allocate a multidimensional array by using dynamic allocation method. However, i heard that it can't be allocated by dynamic allocation. Therefore, i used for loop for allocating. However, it didn't work.
If you answer like this(↓), maybe i won't understand why there is pointer in front of "[wprime]" Please answer the reason why pointer is existed there additionally.
grid = new int* [wprime];
for (int i = 0; i < wprime; i++)
grid[i] = new int[hprime];
How can i solve the problem.. Please, help me b.b
Upvotes: 1
Views: 15108
Reputation: 2684
Addressing your type issues...
As wRAR says, it appears that the problem is with char* a;
needing to be char** a;
. A char*
is a pointer type that refers to a block of memory to be interpreted as char
values, such as in a string. What you seem to need here is a char**
, which is a pointer type that refers to a block of memory to be interpreted as individual char*
values, such as an array of strings.
The other issue is that you need to initialize char** a;
to actually exist, because at the moment it will not point to anything specific. So you will most likely get an access violation that crashes your application. To initialize, do something like char** a = new char* [m];
.
Upvotes: 3
Reputation: 25559
If you want an array of char*
you need to declare it as char**
(the variable a
in your code).
Upvotes: 0