I've got following code fragment and I want to know if it is pure C or it contains some C++ elements. This question stems from the fact that I think it is only C, but some compilers don't accept the code.
// User struct derived from FunctionBlock
struct Function{
// Inputs
int codeGenerationIterator;
int i;
char* s;
// Outputs
// Internal
};
void FunctionCall(struct Function *arg){
}
void FunctionConstructor(struct Function *arg){
arg->i=3;
arg->s="!";
// Call constructor for all not primitive variables
// Create struct with first call
FunctionCall(arg);
}
// User type definition
typedef struct Punto{
int codeGenerationIterator;
Function x[3+1];
Function *x_pointer[3+1];
double y;
};
void PuntoConstructor(struct Punto *arg){
// Call constructor for all not primitive variables
for(arg->codeGenerationIterator=0;arg->codeGenerationIterator<=3;arg->codeGenerationIterator++){
arg->x_pointer[arg->codeGenerationIterator]=&(arg->x[arg->codeGenerationIterator]);
FunctionConstructor(arg->x_pointer[arg->codeGenerationIterator]);
}
}
// User type definition
typedef struct Cerchio{
int codeGenerationIterator;
double r;
Punto centro;
Punto *centro_pointer;
};
void CerchioConstructor(struct Cerchio *arg){
// Call constructor for all not primitive variables
arg->centro_pointer=&(arg->centro);
PuntoConstructor(arg->centro_pointer);
}
// User type definition
typedef struct Container{
int codeGenerationIterator;
Cerchio circonferenza[10+1];
Cerchio *circonferenza_pointer[10+1];
};
void ContainerConstructor(struct Container *arg){
// Call constructor for all not primitive variables
for(arg->codeGenerationIterator=0;arg->codeGenerationIterator<=10;arg->codeGenerationIterator++){
arg->circonferenza_pointer[arg->codeGenerationIterator]=&(arg->circonferenza[arg->codeGenerationIterator]);
CerchioConstructor(arg->circonferenza_pointer[arg->codeGenerationIterator]);
}
}
int main(void){
// Variable definitions
int codeGenerationIterator;
int count;
Punto insiemePunti[50+1];
Punto *insiemePunti_pointer[50+1];
Cerchio cerchio;
Cerchio *cerchio_pointer;
Container container;
Container *container_pointer;
Container containers[11+1];
Container *containers_pointer[11+1];
// Call constructor for all not primitive variables
for(codeGenerationIterator=0;codeGenerationIterator<=50;codeGenerationIterator++){
insiemePunti_pointer[codeGenerationIterator]=&insiemePunti[codeGenerationIterator];
PuntoConstructor(insiemePunti_pointer[codeGenerationIterator]);
}
cerchio_pointer=&cerchio;
CerchioConstructor(cerchio_pointer);
container_pointer=&container;
ContainerConstructor(container_pointer);
for(codeGenerationIterator=0;codeGenerationIterator<=11;codeGenerationIterator++){
containers_pointer[codeGenerationIterator]=&containers[codeGenerationIterator];
ContainerConstructor(containers_pointer[codeGenerationIterator]);
}
container.circonferenza[1].centro.x[0].i=2;
containers[2].circonferenza[2].centro.x[4].i=2;
printf("Works!");
getchar();getchar(); // TODO: delete
return 0;
}
As you can see I haven't use classes or overloading but only simple instructions, structs and some pointers. So, why do some strict C compilers give me an error?
No, this is not valid C. This line:
Function x[3+1];
lacks the struct
keyword, and there's no typedef struct Function Function;
to introduce the type alias you seem to be using.
Also, the use of //
comments requires a recent-enough compiler, that syntax wasn't added to C officially until C99. An older compiler would fail for this reason, too.