指向struct的指针不能正常工作?

时间:2017-12-22 23:48:52

标签: c pointers struct reference

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

这是我的代码和 this是我从

中读取的文字文件

问题是当函数readFile()结束时,totalSphere [1]中的值会丢失,因为您可以看到here,但来自totalSphere [0]的值是正常的。为什么会这样?

2 个答案:

答案 0 :(得分:1)

显然,你在间接水平上迷路了。您在Sphere中分配的readFile个对象数组应该作为(*totalSphere)[i]进行访问。 E.g

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]语法适用于readFile,因为totalSphere在那里Sphere **。在main中,您将访问收到的数组&#34; regular&#34;方式 - 如totalSphere[i]

答案 1 :(得分:0)

以下提议的代码:

  1. 更正评论中列出的问题
  2. 注意以下功能的参数类型:malloc()
  3. 正确检查错误
  4. 纠正了几个逻辑错误
  5. 干净地编译
  6. 将结构定义与该结构的typedef分开
  7. 由于错误退出前正确清理
  8. 将分配的内存传递给free()以避免内存泄漏
  9. 记录了为什么要包含每个头文件
  10. 现在建议的代码:

    #include <stdio.h>   // printf(), fopen(), fclose(), perror(), fscanf(), fprintf()
    #include <stdlib.h>  // exit(), EXIT_FAILURE, malloc(), free()
    
    struct Spheres
    {
        int PositionX;
        int PositionY;
        int Color;
        int Mass;
        int Radius;
        int SpeedX;
        int SpeedY;
    };
    typedef struct Spheres Sphere;
    
    
    // prototypes
    size_t readFile( FILE *file, Sphere **totalSphere );
    
    
    size_t readFile( FILE *file, Sphere **totalSphere )
    {
        int positionX;
        int positionY;
        int color;
        int mass;
        int radius;
        int speedX;
        int speedY;
        size_t amountOfSpheres;
        size_t i;
    
        if( 1 != fscanf( file, "%lu", &amountOfSpheres ) )
        {
            fprintf( stderr, "fscanf for number of spheres failed\n" );
            fclose( file );
            exit( EXIT_FAILURE );
        }
    
        *totalSphere = malloc( amountOfSpheres * sizeof(Sphere) );
        if( !totalSphere )
        {
            perror( "malloc failed" );
            fclose( file );
            exit( EXIT_FAILURE );
        }
    
        for ( i=0; i<amountOfSpheres; i++ )
        {
            if( 7 != fscanf(file, "%d%d%d%d%d%d%d",
                &positionX,
                &positionY,
                &color,
                &mass,
                &radius,
                &speedX,
                &speedY) )
            {
                fprintf( stderr, "fscanf to read fields of sphere failed\n" );
                fclose( file );
                free( totalSphere );
                exit( EXIT_FAILURE );
            }
    
            (*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;
        }
    
        for( size_t j=0; j<amountOfSpheres && j<2; j++ )
        {
            printf("%d %d %d %d %d %d %d\n",
                (*totalSphere)[j]->PositionX,
                (*totalSphere)[j]->PositionY,
                (*totalSphere)[j]->Color,
                (*totalSphere)[j]->Mass,
                (*totalSphere)[j]->Radius,
                (*totalSphere)[j]->SpeedX,
                (*totalSphere)[j]->SpeedY);
        }
    
        return amountOfSpheres;
    }
    
    
    int main( void )
    {
        FILE *file =NULL;
        Sphere *totalSphere = NULL;
    
        if ( !(file = fopen("input.txt","r") ) )
        {
            perror( "fopen failed" );
            exit( EXIT_FAILURE );
        }
    
        size_t x= readFile( file, &totalSphere );
    
        for( size_t i=0; i<x && i<2; i++ )
        {
            printf("%d %d %d %d %d %d %d\n",
                totalSphere[i].PositionX,
                totalSphere[i].PositionY,
                totalSphere[i].Color,
                totalSphere[i].Mass,
                totalSphere[i].Radius,
                totalSphere[i].SpeedX,
                totalSphere[i].SpeedY);
        }
    
        fclose( file );
        free( totalSphere );
        return 0;
    }