I have a structure:
typedef struct stock { const char* key1p2; // stock code const char* key2p2; // short desc const char* desc1; // description const char* prod_grp; // product group const char dp_inqty; // decimal places in quantity const long salprc_u; // VAT excl price const long salprc_e; // VAT includive price const long b_each; // quantity in stock const long b_alloc; // allocated qty const char* smsgr_id; // subgroup const char** barcodes; // barcodes } stock_t;
and I want to initialize arrays of instances of this structure in one line of code for the stock structure.
I tried:
stock_t data_stock[] = { { "0001", "Soup", "Tomato Soup", "71", 0, 100, 120, 10, 0, "", {"12345", "23456", NULL} }, { "0002", "Melon", "Melon and Ham", "71", 0, 200, 240, 10, 0, "", {"34567", "45678", NULL} }, ... { NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL } };
but fail:
data.c:26:74: warning: incompatible pointer types initializing 'const char **' with an expression of type 'char [6]' [-Wincompatible-pointer-types] { "0001", "Soup", "Tomato Soup", "71", 0, 100, 120, 10, 0, "", {"12345", "23456", NULL} }, ^~~~~~~
This barcode field is problematic as it is char **.
(it was clang, but GCC reports a similar error, but less helpful.)
It is almost as if the compiler ignored the curly braces before "12345".
I can get around the problem using:
const char *barcodes0001[] = {"12345", "23456", NULL}; stock_t data_stock[] = { { "0001", "Soup", "Tomato Soup", "71", 0, 100, 120, 10, 0, "", barcodes0001 },
Is the cause of this problem different from char [] and char *, or is there something more subtle. (Perhaps you can initialize arrays of structures, but not array structures.)