0

I am making C console menu program. And I am stuck with the char* assignment. Is there any good method to assign char* to char**? The code is as below:

       char* group0[14]=
{
//group0:5
"Funciton0-1",
"Function0-2",
"Funciton0-3",
"Function0-4",
"Funciton0-5",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};

//... group0~group9 are similiar.

   char* group9[14]=
{
//group9:2
"Funciton9-1",
"Function9-2",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};

    char* name[10][14]=
{
{group0},
{group1},
{group2},
{group3},
{group4},
{group5},
{group6},
{group7},
{group8},
{group9},
};

Why can't I assign those group into this "name" array? The compiler replies me error...

Thank you!

okeyla
  • 65
  • 9
  • What char* assignment? What char**? This code contains neither. And no, you can't assign a char* to a char** or vice versa because they are different, non-compatible types, pointing at different things. – Lundin Aug 15 '18 at 07:00
  • My purpose is to put 1D char array to 2D one. (as you can see group0~9 to name) It seems the type is different, but I take my assignment should be OK... – okeyla Aug 15 '18 at 07:10
  • `char* name[10][14]` is not the same as `char** name[10]`. In your initializer list `{group0}` is same as `{group0, 0, 0, 0...}`as you are initializing arrays. – Gerhardh Aug 15 '18 at 07:59

1 Answers1

2

When you use group0 in the initializer list of name, it will decay to pointer to first element. This means that for expression group0, the type will be of &group0[0], which is char**.

Assuming that name supposed to be array of pointers to groups, you need to change it's type accordingly:

char** name[10] = {
    group0,
    group1,
     ...
user694733
  • 15,208
  • 2
  • 42
  • 68
  • More correct would be to use array pointers `char* (*name)[10] = { &group0, group1, ... };`. – Lundin Aug 15 '18 at 07:51
  • @Lundin Did you mean `char* (*name[10])[14] = { &group0, ...`? I considered that, but I don't think that gives much practical benefit given the complex syntax it brings with it. – user694733 Aug 15 '18 at 08:04
  • Actually I mean `char* (*name)[14]`. No complicated syntax, `name[i]` gives array pointer number `i`, `name[i][j]` gives char* number `j`. – Lundin Aug 15 '18 at 08:05
  • @Lundin I don't think that's enough. `name` needs to be array of 10 elements, if groups 1-9 should fit in. – user694733 Aug 15 '18 at 08:18
  • Ah you are right. So it should indeed be `char* (*name[10])[14] `. – Lundin Aug 15 '18 at 08:26