• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pointer type of 2d array of character pointers

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic