简体   繁体   中英

How do I assign a pointer variable to a different const?

I have a menu pointer that I want to be able to change between 2 different char arrays. My goal is to be able to change the global menu and then be able to call it but i'm having trouble assigning the menu variable.

This is the error I'm getting:

cannot convert 'const char* (*)[11]' to 'char*' in initialization

    const char *chapterSelect[] = {"Chapter 1", "Chapter 2", "Chapter 3", "Chapter 4", "Chapter 5", "Chapter 6", "Chapter 7", "Chapter 8", "Chapter 9", "Chapter 10", "Chapter 11"};
    const char *chapter1[] = {"Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6", "Level 7", "Level 8", "Level 9", "Level 10", "Level 11"};
    char* menu = &chapterSelect;
    drawMenuItems(menu);
    menu = &chapter1;
    drawMenuItems(menu);

void drawMenuItems(const char *Items[])
{}
  • Problem 1: Your use of const is inconsistent.
  • Problem 2: To point at the first item of an array of pointers to char, you need to use (const) char** .
  • Problem 3: &chapterSelect doesn't make any sense since it gives a pointer to the type of the array, not of the first element.

Solution:

const char** menu = chapterSelect;

Or equivalent:

const char** menu = &chapterSelect[0];

The type of &chapterSelect is const char* (*)[11] and since there is no implicit conversion from const char* (*)[11] to a char* , we get the mentioned error. Basically, there is a type mismatch on the left and right side.

To solve this you can make sure that the type on the lhs and rhs are same:

//-----------------vvvvvvvvvvvvvvvvvvvvvv-------->changed type to this
void drawMenuItems(const char*(*Item)[11])
{ 
}
int main()
{
    const char *chapterSelect[] = {"Chapter 1", "Chapter 2", "Chapter 3", "Chapter 4", "Chapter 5", "Chapter 6", "Chapter 7", "Chapter 8", "Chapter 9", "Chapter 10", "Chapter 11"};
    const char *chapter1[] = {"Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6", "Level 7", "Level 8", "Level 9", "Level 10", "Level 11"};
//--vvvvvvvvvvvvvvvvvvvvvv------------------->changed type to this 
    const char*(*menu)[11] = &chapterSelect;
    drawMenuItems(menu); //or drawMenuItems(&chapterSelect);
    menu = &chapter1;
    drawMenuItems(menu); // or drawMenuItems(&chapter1);
}

working demo


C++20

Note that with C++20 we're allowed to do this for array of different sizes. This means that you can change menu 's declaration to:

//---------------------v---------------------->works with C++20, NO SIZE GIVEN HERE!!
    const char*(*menu)[] = &chapterSelect;

c++ 20 demo

I will start with answering your question but will follow with additional suggestion:

The error you see is because you are taking an address of const char* which in practice means the returned value should be const char** .

Instead of:

char* menu = &chapterSelect;

use:

char** menu = (char**) &chapterSelect;

Note there are 2 points of change. One for menu type and one for explicit const-cast-away.

Although the above will solve the compilation error - I want to raise a few suggestions:

  1. I don't understand the need for char** menu as you can pass the arrays' addresses directly to drawMenuItems() . If the function that from its name means it prints the array as menu - then it shouldn't alter the array and should receive a const char** .
  2. It is not good practice to cast-away const. The compiler is considering you did a coding mistake and the right side const-cast-away is telling both compiler and other developers that you are casting-away-const on purpose.
  3. When your code includes a call to a function ( drawMenuItems() ), it is good to provide at least the function declaration for clearer understanding of the readers.

I hope this resolves the issue raised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM