I have a two dimensional array where the first dimension has a variable length but the second dimension is fixed. Now in a function call I could do something like char foo[][3]
but what is the corresponding definition in a struct
?
So in the example code I expected it to print each string in a line, but as expected it treats the stored pointer as a single dimensional array.
#include <stdlib.h>
#include <stdio.h>
struct payload_s {
size_t length;
char *text;
};
typedef struct payload_s payload;
static char some_text[4][3] = {"Ab", "Cd", "Ef", "Gh"};
payload* create_payload(char *text, size_t length)
{
payload *p = malloc(sizeof *p);
p->text = text;
p->length = length;
return p;
}
int main()
{
payload *p = create_payload(some_text, 4);
for (size_t i = 0; i < p->length; ++i)
printf("%zu: %s\n", i, &p->text[i]);
}
I mainly noticed this because of a warning:
strut.c: In function ‘main’:
strut.c:23:33: warning: passing argument 1 of ‘create_payload’ from incompatible pointer type [-Wincompatible-pointer-types]
payload *p = create_payload(some_text, 4);
^~~~~~~~~
strut.c:13:10: note: expected ‘char *’ but argument is of type ‘char (*)[3]’
payload* create_payload(char *text, size_t length)
^~~~~~~~~~~~~~
I can get rid of this warning when the function is actually defined as payload* create_payload(char text[][3], size_t length)
, but then there is a warning a few lines later and the behavior didn't change:
strut.c: In function ‘create_payload’:
strut.c:16:13: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
p->text = text;
^
Is the only solution to manually increment the pointer by the length of each value?