我有一个作业,我必须将类似c ++的程序转换为ac程序。
如果我有类似的东西
class B {
int var;
int somefunction(){
some code here
}
}
Run Code Online (Sandbox Code Playgroud)
它会变成
struct B{
int var;
}
int somefunction(){
some code here
}
Run Code Online (Sandbox Code Playgroud)
基本上,我不得不改变class,以struct每次看到它的时候,如果有一个功能,我现在把它移到了外面结构。
做这样的事情的最佳方法是什么?我得到了背后的理论,但不确定如何去实现它。
我有两个简单的结构,在我的主要创建中:
struct Car *myCar[200]
struct Car *otherCar[200]
Run Code Online (Sandbox Code Playgroud)
当我第一次尝试初始化它们时,我试过:
for (int i = 0; i < 200; i++){
myCar[i] = malloc(sizeof(struct Car*));
otherCar[i] = malloc(sizeof(struct Car*))
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.但如果我将它们拆分成单独的循环,如:
for (int i = 0; i < 200; i++){
myCar[i] = malloc(sizeof(struct Car*));
}
for (int x = 0; x < 200; x++){
otherCar[x] = malloc(sizeof(struct Car*))
}
Run Code Online (Sandbox Code Playgroud)
有用.有人可以向我解释为什么它在每个例子中都不起作用吗?