1

I am trying to access the first element inside the array 'levels'.

unsigned char Level1[70] =
{
    0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x00,0x01,0x01,0x01,0x01,0x01,0x03,0x07,0x02,0x02,0x01,0x01,0x01,0x01,0x01,0x01,0x03,0x03,0x02,0x02,0x01,
    0x01,0x01,0x0b,0x01,0x01,0x01,0x01,0x02,0x02,0x01,0x08,0x01,0x01,0x01,0x01,0x01,0x01,0x02,0x02,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x0f,0x02,0x02,0x02,0x02,0x02,
    0x02,0x02,0x02,0x02,0x02,0x02
};

unsigned char (*levels)[1] = {&Level1};

I have tried things like:

unsigned char curr_level[70] = levels[0];

but they have not worked.

Thank you!

3
  • 1
    Huh? I only see one array. Commented Apr 29, 2020 at 0:26
  • levels[0] is a pointer to an array, not an array. Commented Apr 29, 2020 at 0:27
  • If 70 is a meaningful number it should be a #define constant of some kind. Commented Apr 29, 2020 at 0:35

1 Answer 1

1

WARNING: this answer may be INCORRECT See comments for details.

According to your declaration,

unsigned char (*levels)[1] = {&Level1};

levels is an array of char pointers, and therefore levels[0] is a char pointer.
So depending on what you want to do, you may try one of the followings:

unsigned char *curr_level=levels[0];

or

unsigned char *curr_level;
curr_level=malloc(sizeof(Level1)); /* Don't forget to call free() on it later */
memcpy(curr_level,levels[0],sizeof(Level1));
Sign up to request clarification or add additional context in comments.

2 Comments

unsigned char (*levels)[1] declares levels to be a pointer to an array of 1 unsigned char, not to be an array of “char pointers”. levels[0] is an array of 1 unsigned char; it is not a “char pointer”, although, in many uses, it will be converted to a pointer to its element.
@eric Thank you. Is it because of the parentheses(or would there be any difference if it is unsigned char *levels[])? And according to your comment, it is actually incorrect to declare like that (since one is misusing an array as a pointer)?

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.