2

I need help with a pointer declaration, I have several classes with arrays of pointers eg.

const char* const clsMainWin::mcpszXMLattrRoot[] = {"bottom","left","right","top",NULL};
const char* const clsMainWin::mcpszXMLattrA[] = {"x","y","z",NULL};

What I want to do is define a pointer which groups the above arrays of pointers allowing me to select one of the about with a single index, eg:

const char* const allOptions[] = {mcpszXMLattrRoot, mcpszXMLattrA};

Where:

allOptions[0][...] would be used to access mcpszXMLattrRoot and its contents:
allOptions[1][...] would be used to access mcpszXMLattrA and its contents

However so far I'm struggling to get the declaration of allOptions correct.

2
  • You can't really do that, because mcpszXMLattrRoot and mcpszXMLattrA are different types, which means you can't collect them into the same array. The type of mcpszXMLattrRoot is const char * const [5], while the type of mcpszXMLattrA is const char * const [4]. The number of elements in the arrays are part of the type, and an array can only contain elements of the same type. Commented Apr 1, 2016 at 7:51
  • this is a C++ question, not a C question. please remove the c tag Commented Apr 1, 2016 at 17:12

3 Answers 3

2

You could store the pointer of mcpszXMLattrRoot and mcpszXMLattrA (i.e. const char * const *.

const char* const * allOptions[] = {mcpszXMLattrRoot, mcpszXMLattrA};
Sign up to request clarification or add additional context in comments.

Comments

2
static const char* const a[] = {"a1", "a2", "a3"};
static const char* const b[] = {"b1", "b2", "b3", "b4"};
static const char* const* const z[] = {a, b};

The type of z has an additional * because it is an array of pointers to arrays of pointers of char.

You need const before the latter * because a and b are const. Without constantness it would be:

static const char* a[] = {"a1", "a2", "a3"};
static const char* b[] = {"b1", "b2", "b3", "b4"};
static const char** z[] = {a, b};

You can initialize it in-class with constexpr

class Foo {
    static constexpr const char* const a[] = {"a1", "a2", "a3"};
    static constexpr const char* const b[] = {"b1", "b2", "b3", "b4"};
    static constexpr const char* const* z[] = {a, b};
};

Or out-class:

class Foo {
    static const char* const a[];
    static const char* const b[];
    static const char* const* z[];
};

const char* const Foo::a[] = {"a1", "a2", "a3"};
const char* const Foo::b[]= {"b1", "b2", "b3", "b4"};
const char* const* Foo::z[] = {a, b};

2 Comments

Trying this now: const char* const * clsMainWin::mcppAttributes[]
This answer won't allow accessing with a single index.
0

This can be achieved as:

class clsMainWin{
    static const char* const mcpszXMLattrRoot[5];
    static const char* const mcpszXMLattrA[4];
    static const char* const *allOptions[2];

    // And other things....

};

const char* const clsMainWin::mcpszXMLattrRoot[] = {"bottom","left","right","top",NULL};
const char* const clsMainWin::mcpszXMLattrA[] = {"x","y","z",NULL};
const char*const * clsMainWin::allOptions[2] = {mcpszXMLattrRoot, mcpszXMLattrA};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.