指向struct的指针不';你不能正常工作吗? typedef结构球体{ int-PositionX; 内部位置; 内色; 整数质量; 整数半径; int SpeedX; 国际快速; }球体; char readFile(文件*文件,球体**totalSphere){ int positionX,positionY,颜色,质量,半径,speedX,speedY,amountOfSpheres,i; fscanf(文件“%d”和amountOfSpheres); *totalSphere=malloc(amountOfSpheres*sizeof(Sphere)); for(i=0;i是从中读取的文本文件

指向struct的指针不';你不能正常工作吗? typedef结构球体{ int-PositionX; 内部位置; 内色; 整数质量; 整数半径; int SpeedX; 国际快速; }球体; char readFile(文件*文件,球体**totalSphere){ int positionX,positionY,颜色,质量,半径,speedX,speedY,amountOfSpheres,i; fscanf(文件“%d”和amountOfSpheres); *totalSphere=malloc(amountOfSpheres*sizeof(Sphere)); for(i=0;i是从中读取的文本文件,c,pointers,struct,reference,C,Pointers,Struct,Reference,问题是,当函数readFile()结束时,正如您所看到的,来自totalSphere[1]的值丢失,但来自totalSphere[0]的值丢失正常。为什么会发生这种情况?显然,您在间接层次中迷失了方向。您在readFile中分配的Sphere对象数组应该作为(*totalSphere)[i]访问 typedef struct Spheres{ int PositionX; int PositionY; int Color; int Mass; int Ra

问题是,当函数readFile()结束时,正如您所看到的,来自totalSphere[1]的值丢失,但来自totalSphere[0]的值丢失正常。为什么会发生这种情况?

显然,您在间接层次中迷失了方向。您在
readFile
中分配的
Sphere
对象数组应该作为
(*totalSphere)[i]
访问

typedef struct Spheres{
    int PositionX;
    int PositionY;
    int Color;
    int Mass;
    int Radius;
    int SpeedX;
    int SpeedY;
}Sphere;

char readFile(FILE *file,Sphere **totalSphere){
    int positionX,positionY,color,mass,radius,speedX,speedY,amountOfSpheres,i;
    fscanf(file,"%d",&amountOfSpheres);
    *totalSphere=malloc(amountOfSpheres*sizeof(Sphere));
    for (i=0;i<amountOfSpheres;i++){
        fscanf(file,"%d%d%d%d%d%d%d",&positionX,&positionY,&color,&mass,&radius,&speedX,&speedY);
        totalSphere[i]->PositionX=positionX;
        totalSphere[i]->PositionY=positionY;
        totalSphere[i]->Color=color;
        totalSphere[i]->Mass=mass;
        totalSphere[i]->Radius=radius;
        totalSphere[i]->SpeedX=speedX;
        totalSphere[i]->SpeedY=speedY;
    }
    printf("%d %d %d %d %d %d %d\n",totalSphere[0]->PositionX,totalSphere[0]->PositionY,totalSphere[0]->Color,totalSphere[0]->Mass,totalSphere[0]->Radius,totalSphere[0]->SpeedX,totalSphere[0]->SpeedY);
    printf("%d %d %d %d %d %d %d\n",totalSphere[1]->PositionX,totalSphere[1]->PositionY,totalSphere[1]->Color,totalSphere[1]->Mass,totalSphere[1]->Radius,totalSphere[1]->SpeedX,totalSphere[1]->SpeedY);
}


int main()
{
    FILE *file;
    Sphere *totalSphere;
    totalSphere=NULL;
    if ((file=fopen("input.txt","r"))!=NULL){
        if (readFile(file,&totalSphere)){
            printf("%d %d %d %d %d %d %d\n",totalSphere[0].PositionX,totalSphere[0].PositionY,totalSphere[0].Color,totalSphere[0].Mass,totalSphere[0].Radius,totalSphere[0].SpeedX,totalSphere[0].SpeedY);
            printf("%d %d %d %d %d %d %d\n",totalSphere[1].PositionX,totalSphere[1].PositionY,totalSphere[1].Color,totalSphere[1].Mass,totalSphere[1].Radius,totalSphere[1].SpeedX,totalSphere[1].SpeedY);
            fclose(file);
    return 0;
}
(i=0;i{ fscanf(文件“%d%d%d%d%d%d%d”、&positionX、&positionY、&color、&mass、&radius、&speedX、&speedY); (*totalSphere)[i].PositionX=PositionX; (*totalSphere)[i].PositionY=PositionY; ... 您的原始版本不正确


(*totalSphere)[i]
语法适用于
readFile
内部,因为
totalSphere
在那里是
Sphere**
。在
main
中,您将以“常规”方式访问接收到的数组,如
totalSphere[i]
所建议的以下代码:

for (i = 0; i < amountOfSpheres; i++) {
  fscanf(file, "%d%d%d%d%d%d%d", &positionX, &positionY, &color, &mass, &radius, &speedX, &speedY);
  (*totalSphere)[i].PositionX = positionX;
  (*totalSphere)[i].PositionY = positionY;
  ...
  • 更正问题注释中列出的问题
  • 注意函数的参数类型,如:
    malloc()
  • 正确检查错误
  • 修正了几个逻辑错误
  • 干净地编译
  • 将结构定义与该结构的
    typedef
    分开
  • 由于错误,请在退出之前正确清理
  • 将分配的内存传递给
    free()
    ,以避免内存泄漏
  • 记录包含每个头文件的原因
  • 现在建议的守则是:

    for (i = 0; i < amountOfSpheres; i++) {
      fscanf(file, "%d%d%d%d%d%d%d", &positionX, &positionY, &color, &mass, &radius, &speedX, &speedY);
      (*totalSphere)[i].PositionX = positionX;
      (*totalSphere)[i].PositionY = positionY;
      ...
    
    #包括//printf()、fopen()、fclose()、perror()、fscanf()、fprintf()
    #包括//exit()、exit_FAILURE、malloc()、free()
    结构球
    {
    int-PositionX;
    内部位置;
    内色;
    整数质量;
    整数半径;
    int SpeedX;
    国际快速;
    };
    typedef结构球体;
    //原型
    大小\u t读取文件(文件*文件,球体**总球体);
    大小\u t读取文件(文件*文件,球体**totalSphere)
    {
    int-positionX;
    内部位置;
    内色;
    整数质量;
    整数半径;
    int speedX;
    国际快速;
    球的大小和数量;
    尺寸i;
    if(1!=fscanf(文件“%lu”、&amountOfSpheres))
    {
    fprintf(stderr,“失败球体数的fscanf\n”);
    fclose(文件);
    退出(退出失败);
    }
    *totalSphere=malloc(amountOfSpheres*sizeof(Sphere));
    如果(!totalSphere)
    {
    perror(“malloc失败”);
    fclose(文件);
    退出(退出失败);
    }
    对于(i=0;iPositionX=positionX;
    (*totalSphere)[i]->PositionY=PositionY;
    (*totalSphere)[i]->Color=Color;
    (*totalSphere)[i]->质量=质量;
    (*totalSphere)[i]->半径=半径;
    (*totalSphere)[i]->SpeedX=SpeedX;
    (*totalSphere)[i]->SpeedY=SpeedY;
    }
    对于(尺寸j=0;位置y,
    (*totalSphere)[j]->颜色,
    (*总球)[j]->质量,
    (*totalSphere)[j]->半径,
    (*totalSphere)[j]->SpeedX,
    (*totalSphere)[j]->SpeedY);
    }
    返回数量圈;
    }
    内部主(空)
    {
    FILE*FILE=NULL;
    Sphere*totalSphere=NULL;
    如果(!(file=fopen(“input.txt”,“r”))
    {
    perror(“fopen失败”);
    退出(退出失败);
    }
    size\u t x=readFile(file和totalSphere);
    
    对于(size_t i=0;iInside
    readFile
    它应该是
    (*totalSphere)[i]。PositionX=PositionX
    而不是
    totalSphere[i]->PositionX=PositionX
    。诸如此类。在
    readFile
    中的任何地方,您都应该使用
    (*totalSphere)[i]。
    ,而不是
    totalSphere[i]->
    。后者完全不正确。这甚至不应该编译。main的“totalSphere”是指向结构的指针,您在readFile中访问它时就像它是指向指针的指针一样。您的“->”必须是“.”。打开所有编译器警告并侦听它们!合理地命名您的变量……您必须将变量命名为“totalSphere”这不仅是不一样的东西,它们的类型也不一样。@Lee Daniel Crocker:
    main
    s
    totalSphere
    被传递到
    readFile
    作为
    &totalSphere
    并在
    readFile
    中作为
    Sphere**totalSphere
    接收。这里没有问题-它应该编译得很好。但是里面的代码e> readFile
    毫无意义。看来作者只是想不惜一切代价“使其编译”,而不关心代码的含义。为了便于阅读和理解:1)在打印页上保留右边距。建议在逗号后插入新行,并缩进以下行发布的代码无法编译!它缺少所需头文件所需的include语句。此外,函数
    main()
    缺少两个右括号“}”。注意:在C中,缩进完全没有任何意义(超出可读性)