0
\$\begingroup\$

I'm having trouble assigning variables to structs, particularly making an array of structs, with the XC8 compiler.

Example code below:

typedef struct p{
  int id;
  dateStamp start;
  dateStamp stop;
  int parent_id;
} period;

period p1 = {1, {0, 0, 0, 1, 1, 14}, {20, 0, 0, 1, 1, 14}, 1};
period p2 = {1, {21, 0, 0, 1, 1, 14}, {50, 0, 0, 1, 1, 14}, 1};
period p3 = {1, {51, 0, 0, 1, 1, 14}, {59, 0, 0, 1, 1, 14}, 1};

period periods[3] = {p1, p2, p3};

And this is the compiler error log:

main.c:233: error: (188) constant expression required
main.c:233: error: (207) simple type required for "@"
main.c:233: error: (182) illegal conversion between types
struct p -> int
main.c:233: error: (181) non-scalar types can't be converted to other types
main.c:233: error: (188) constant expression required
main.c:233: error: (207) simple type required for "@"
main.c:233: error: (182) illegal conversion between types
struct p -> int
main.c:233: error: (181) non-scalar types can't be converted to other types
main.c:233: error: (188) constant expression required
main.c:233: error: (207) simple type required for "@"
main.c:233: error: (182) illegal conversion between types
struct p -> int
main.c:233: error: (181) non-scalar types can't be converted to other types
main.c:256: warning: (345) unreachable code
(908) exit status = 1
make[2]: *** [build/default/production/main.p1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

Suggestions would be welcome....

\$\endgroup\$
  • \$\begingroup\$ I suggest you start with a simpler example. \$\endgroup\$ – Dave Tweed Jan 18 '14 at 15:18
  • \$\begingroup\$ This would possibly fit better on a programming stack, as it relates to the usage of C. \$\endgroup\$ – David Jan 18 '14 at 15:21
  • \$\begingroup\$ @david I wasn't sure but went for this stack in the end as my understanding is XC8 is a compiler just for PIC... mods feel free to close though if you disagree \$\endgroup\$ – SimonBarker Jan 18 '14 at 15:53
3
\$\begingroup\$

This is a C problem.

period periods[3] = {p1, p2, p3};

is invalid in C, as p1, p2 & p3 are struct variables, and not constants known at compile-time, therefore you cannot use them as initializers.

You may use macros to substitute the same constant values into p1, p2, p3 and periods[3] so that you don't have to duplicate the values:

#define P1 {1, {0, 0, 0, 1, 1, 14}, {20, 0, 0, 1, 1, 14}, 1}
#define P2 {1, {21, 0, 0, 1, 1, 14}, {50, 0, 0, 1, 1, 14}, 1}
#define P3 {1, {51, 0, 0, 1, 1, 14}, {59, 0, 0, 1, 1, 14}, 1}

period p1 = P1;
period p2 = P2;
period p3 = P3;

period periods[3] = { P1, P2, P3 };

Or you may simply avoid initializers altogether, and use explicit statements at the beginning of your program to initialize these arrays.

\$\endgroup\$
0
\$\begingroup\$

It seems changing the code to:

period periods[3];

periods[0] = p1;
...
...

solves the problem - XC8 must not be able to deal with the shorthand.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.