• Post Reply Bookmark Topic Watch Topic
  • New Topic

pointer type of 2d array of character pointers  RSS feed

 
ravindra koranga
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would i declare a pointer which points to 2d array of character pointers. like
char *ar[10][10];
In my understanding this array is stored as array of array, so ar points to array of pointers , each of which points to a column in this array. So it has three level of pointers. So it should be declared as
char ***p;
Thus both ar and p are of same type.
But if I use this p like 2d array for e.g. p[0][0], it gives error as segmentation fault. Why does this happen and what would be the correct way to declare p?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ravindra koranga wrote:
In my understanding this array is stored as array of array, so ar points to array of pointers , each of which points to a column in this array. So it has three level of pointers. So it should be declared as
char ***p;


Unfortunately, this is *not* true. This is how the Java Language does "multidimensional arrays", but with C/C++, multidimension arrays are actually that (and not array of arrays). So, the closest equivalent would be*...And to get to the zero row zero column element...
Henry

Note: I would not be surprised, if, with a later version of C/C++, there is a way to declare the pointer without losing the ranges of the indexes. However, for me, I still have to be compatible with older C/C++ compilers, so do stuff as described.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!