macros - Initialize array after declaring in C -
i trying following getting following error:
"error: expected expression before
{
token
test_stubs.h
#define signature 0x78,0x9c
test.c
#include "test_stubs.h" static unsigned myarray[100]; static void setup(void) { myarray = {signature}; }
edit
follow on question:
is there way assign individual values in #define
specific indexes of myarray? instance...
#include "test_stubs.h" static unsigned myarray[100]; static void setup(void) { myarray[0] = signature[0]; //0x78 myarray[1] = signature[1]; //0x9c }
clearly above code not code signature neither array or pointer.
as per c
syntax rules, can initialize array using brace enclosed initializer list @ time of definition.
afterwards, have initialize element element, using loop, or, if need initialize elements same value, can consider using memset()
.
edit:
no, mentioned in comments, per code snippet, signature
neither array name, nor represent array type, cannot use indexing on that.
however, using compound literal (on , above c99
), if change #define
, then, somehow, can make work. see below code example,
#include <stdio.h> #define signature ((unsigned [100]){0x78,0x9c}) static unsigned myarray[100]; int main (void) { myarray[0] = signature[0]; //0x78 myarray[1] = signature[1]; //0x9c return 0; }
see live version
Comments
Post a Comment