Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

pointer type of 2d array of character pointers

 
ravindra koranga
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?
 
Henry Wong
author
Posts: 23912
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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic