Hi,
I made a small C program that compiles fine locally on my computer using gcc, but compiling for Arduino generates errors like:
error: variable or field 'initArray' declared void
error: 'Monster' was not declared in this scope
error: 'a' was not declared in this scope
...and more of the same errors
I reduced the code to only these declarations of structs and functions, which generate the errors:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
uint8_t value1;
uint8_t value2;
uint8_t visited;
} edge;
typedef struct {
edge *edges;
uint8_t used;
uint8_t size;
} Monster;
void initArray(Monster *a, uint8_t initialSize) {
a->edges = (edge *)malloc(initialSize * sizeof(edge));
a->used = 0;
a->size = initialSize;
}
void insertArray(Monster *a, edge element) {
if (a->used == a->size) {
a->size *= 2;
a->edges = (edge *)realloc(a->edges, a->size * sizeof(edge));
}
a->edges[a->used++] = element;
}
void setup() {}
void loop() {}
What am I doing wrong?
Any help would be much appreciated.
It compiles for me. IDE 1.6.6
aarg:
It compiles for me. IDE 1.6.6
Good to know.
I'm using Sublime Text and the Stino plugin as my IDE, so I'll look into that tomorrow.
I get the errors using IDE V1.6.5.
(I vaguely remember seeing this problem once before, but can't remember how it was solved.)
Huh - didn't know you could make an anonymous struct and use it in a typedef.
In any case - maybe giving the struct a name would help:
typedef struct _Monster {
edge *edges;
uint8_t used;
uint8_t size;
} Monster;
It indeed compiled correctly on the original Arduino IDE 1.6.8.
What fixed it for my IDE was making the following changes:
struct _edge;
struct _Monster;
typedef struct _edge {
uint8_t value1;
uint8_t value2;
uint8_t visited;
} edge;
typedef struct _Monster {
edge *edges;
uint8_t used;
uint8_t size;
} Monster;
void initArray(struct _Monster *a, uint8_t initialSize) {
a->edges = (edge *)malloc(initialSize * sizeof(edge));
a->used = 0;
a->size = initialSize;
}
void insertArray(struct _Monster *a, struct _edge element) {
if (a->used == a->size) {
a->size *= 2;
a->edges = (edge *)realloc(a->edges, a->size * sizeof(edge));
}
a->edges[a->used++] = element;
}
This comment from a ~10 year old thread was my solution: syntax driving me crazy - #17 by soundanalogous - Syntax & Programs - Arduino Forum