-2

I have a function like this:

void Menu_show(uint8_t count, char items[][UI_COLS + 1]);

where count is the length of the first dimension of the array items. I call this function in this way:

#define MAX_SIZE 4
char items[MAX_SIZE ][UI_COLS + 1];

for (uint8_t i = 0; i < MAX_SIZE ; i++) sprintf(items[i], "%u", i);
...
Menu_show(MAX_SIZE, items);

It works, but now I need to store a pointer to the first element of the array in my function.

My tougths:

  1. the first element is items[0][0]
  2. its pointer is &items[0][0]
  3. but now I have a pointer to a char, not to a bidimensional array

In fact, something like printf(items[3]) doesn't work in this way. Then, I thought that the pointer to the first element of the second (fixed) dimension is &item[0] but:

char *_items[UI_COLS + 1];
_items = &items[0];

gives an error:

assignment to expression with array type

I don't understand the right syntax to keep the bidimensional array usage when store the first location in a pointer.

Mark
  • 4,338
  • 7
  • 58
  • 120
  • 1
    What you present *shouldn't* work (correctly), and your compiler ought to be warning you about mismatched pointer types. Specifically, the function parameter `items` has type `char (*)[UI_COLS]`, whereas the actual argument has type `char (*)[UI_COLS + 1]`. These are not compatible, and the difference is significant. – John Bollinger Nov 13 '17 at 20:41
  • You're talking about the first snippet? Indeed it works (avrgcc). Of course the two lines below don't work - as said. Ah ok, you're talking about the missing +1, fixed. Apologies. – Mark Nov 13 '17 at 20:43
  • 1
    Is this a code-dodging exercise that changes the code posted according to comments? Where is the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that shows the problem? – Weather Vane Nov 13 '17 at 20:46
  • It was a typo. I have a screenshot of my code. I cannot do copy-paste right now. Sorry, but the minimal code is there... I only need to store a pointer. How to improve my question? – Mark Nov 13 '17 at 20:47
  • I really don't understand the down-votes. There are plenty of very easy questions with a lot of votes, even without any piece of code. I'm just trying to learn from you experts. – Mark Nov 13 '17 at 20:59
  • 1
    @Mark. I don't understand votes either. I've only been on this site for a few weeks, but I've already noticed "easy" question and answers in other languages get up-voted, but similar C questions get downvoted. I wouldn't take it personally. I still learn in both cases -- which is why I'm on here. – MFisherKDX Nov 13 '17 at 21:17

2 Answers2

2

The way you defined _items is as an array of char *. You're attempting to assign to that array (which by itself is illegal) a pointer to an array.

Since each element of items is an array of type char [UI_COLS + 1], you need to save its address in a pointer to an array:

char (*_items)[UI_COLS + 1] = &items[0];
printf("_items=%s\n", *_items);
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Hence my mistake was the missing parenthesis around *_items*? – Mark Nov 13 '17 at 20:50
  • 1
    @Mark Correct. The array subscript operator has higher precedence, so the parenthesis are required to make the address-of operator bind to the name first. – dbush Nov 13 '17 at 20:52
0

Arrays can be interpreted as a pointer If you have

arr[][]

arr can be considered

&arr[0]

and

arr[i] 

can be considered as

&arr[i][0]

A two dimensional array doesn't really have a first element, except that it's first element array[0] is the first array in that twodimensional array.

printf(items[3]);

should work, because

items[3] 

is a char array.

P.S.

For security reasons use

printf("%s",items[3]);

instead.

Unh0lys0da
  • 196
  • 8