簡體   English   中英

指向結構的指針不能正常工作?

[英]Pointer to struct doesn't work properly?

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

這是我的代碼, 是我正在讀取的文本文件

問題在於,當函數readFile()結束時,如您在此處看到的,totalSphere [1]的值會丟失,但是totaltal [0]的值是可以的。 為什么會這樣呢?

顯然,您在間接級別上迷路了。 您在readFile分配的Sphere對象數組應該以(*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;
  ...

您的原始版本不正確。

(*totalSphere)[i]語法適用於readFile ,因為totalSphereSphere ** main您將以“常規”方式訪問接收到的數組-為totalSphere[i]

以下建議的代碼:

  1. 更正問題注釋中列出的問題
  2. 注意函數的參數類型,例如: malloc()
  3. 正確檢查錯誤
  4. 糾正幾個邏輯錯誤
  5. 干凈地編譯
  6. 將結構定義與該結構的typedef分開
  7. 由於錯誤而正確清理,然后退出
  8. 將分配的內存傳遞給free()以避免內存泄漏
  9. 說明為什么要包含每個頭文件

現在建議的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM