c - define constant variable in a structure initiation -
i tried follow following link in order initiate data:
struct dmparam { char *p; char *v; }; struct dmobj { int a; int b; const struct dmparam * const *dmparam; }; const struct dmobj dmobj[] = { {1, 11, null}, {2, 22, (struct dmparam * const []) {//params {"p1", "v1"}, {"p2", "v2"}, }//params }, {3, 33, null}, };
but got warnings in compilation
test.c:35:3: warning: braces around scalar initializer [enabled default] {"p1", "v1"}, ^ test.c:35:3: warning: (near initialization ‘(anonymous)[0]’) [enabled default] test.c:35:3: warning: initialization incompatible pointer type [enabled default] test.c:35:3: warning: (near initialization ‘(anonymous)[0]’) [enabled default] test.c:35:3: warning: excess elements in scalar initializer [enabled default] test.c:35:3: warning: (near initialization ‘(anonymous)[0]’) [enabled default] test.c:36:3: warning: braces around scalar initializer [enabled default] {"p2", "v2"}, ^ test.c:36:3: warning: (near initialization ‘(anonymous)[1]’) [enabled default] test.c:36:3: warning: initialization incompatible pointer type [enabled default] test.c:36:3: warning: (near initialization ‘(anonymous)[1]’) [enabled default] test.c:36:3: warning: excess elements in scalar initializer [enabled default] test.c:36:3: warning: (near initialization ‘(anonymous)[1]’) [enabled default] test.c:38:2: warning: initialization incompatible pointer type [enabled default] }, ^ test.c:38:2: warning: (near initialization ‘dmobj[1].dmparam’) [enabled default]
what m missing?
you missing 2 indexatation levels. dmobj::dmparam
(forgetting const moment struct dmparam **
pointer (const) pointer (const) dmparam
.
instead of initializing pointer pointer, compiler tried initialize directly array of dmparam
. clang warning explicit : incompatible pointer types initializing 'struct dmparam *const' expression of type 'char [3]'. compiler not clever enough guess wanted...
you must explicit here:
const struct dmparam params [] = { {"p1", "v1"}, {"p2", "v2"}, }; const struct dmparam * const p = params; const struct dmobj dmobj[] = { {1, 11, null}, {2, 22, &p }, {3, 33, null}, };
here params
array of const dmparam
, p
const pointer params
(another variable), , put address of p
in dmobj[1]
.
Comments
Post a Comment