gpt4 book ai didi

代码片段 : is this pure C?

转载 作者:太空宇宙 更新时间:2023-11-04 05:05:40 26 4
gpt4 key购买 nike

我有以下代码片段,我想知道它是纯 C 还是包含一些 C++ 元素。这个问题源于我认为它只是 C,但有些编译器不接受代码。

   // 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;
}

如您所见,我没有使用类或重载,只使用了简单的指令、结构和一些指针。那么,为什么一些严格的 C 编译器会给我一个错误?

最佳答案

不,这不是有效的 C。这一行:

    Function x[3+1];

缺少 struct 关键字,也没有 typedef struct Function Function; 来引入您似乎正在使用的类型别名。

此外,// 注释的使用需要足够新的编译器,该语法直到 C99 才正式添加到 C 中。较旧的编译器也会因这个原因而失败。

关于代码片段 : is this pure C?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13859251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com