简体   繁体   English

C + Malloc中的结构

[英]Struct in C + Malloc

I have this [UPDATED]我有这个[更新]

typedef struct Cerchi { 
    char nome[4];
    int x; //coordinate centro
    int y; //coordinate centro
    int r; //raggio
}cerchio;

   cerchio *leggi_file ( FILE *fp)
{
            char buffer[100]; 
            int i=0;    
            cerchio *bufcer;
            bufcer=(cerchio *)malloc(sizeof (int)*10000000);               
                while(fgets(buffer, sizeof(buffer), fp)!= NULL) //Fino a che file non è null
                         {  
                         //bufcer=realloc(bufcer, sizeof(int)*100);
                         sscanf(buffer, "%s %d %d %d",bufcer[i].nome,&bufcer[i].x,&bufcer[i].y,&bufcer[i].r);
        /*stampa di controllo*/          printf("\n%s %d %d %d",bufcer[i].nome,bufcer[i].x,bufcer[i].y,bufcer[i].r); 
                         i++;                
                         }
                         return bufcer;


}

This function is working.此功能正在运行。 That's the UPDATED main这是更新的主要内容

int main(int argc, char *argv[]) {
    FILE *fp;
if (argc < 2) {
    printf("Mancano parametri da tastiera\n"); //Sempre >=1 parametri passati
    exit(1);
}
fp = fopen(argv[1], "r");
if (fp == NULL) {
    printf("Impossibile aprire il file\n");
    exit(1);
}
    struct Cerchi *bufcer = NULL;
bufcer = leggi_file(fp);
stampa(bufcer); 
//vettore = leggi_file(FILE *fp);    E R R O R E
fclose(fp);

return 0;
}

I'm not able to print my *bufcer struct in this function我无法在这个函数中打印我的 *buffer 结构

    void stampa(bufcer)
{
int i;
for (i=0;i<50;i++)
 {
 printf("\n%s %d %d %d",bufcer[i]->nome,bufcer[i]->x,bufcer[i]->y,bufcer[i]->r); 
 }
}

Please try to help me , tomorrow morning i have an exam about it [UPDATED] Wich error i'm doing?请试着帮助我,明天早上我有一个关于它的考试 [更新] 我在做什么错误? Can you try to solve it and help me?你能试着解决它并帮助我吗? Thanks a lot....非常感谢....

You have to use -> operator when accessing structure elements, so your printf will be as访问structure元素时必须使用->运算符,因此您的printf将是

printf("\\n%s %d %d %d",bufcer[i]->nome,bufcer[i]->x,bufcer[i]->y,bufcer[i]->r);

Also, -> operator use for scanf()此外, ->运算符用于scanf()

You need to pass & of structure to pass structure pointer您需要通过& of 结构来传递结构指针

  1. Do not cast return value of malloc() . 不要转换malloc()返回值
  2. Always check the return value of sscanf() to ensure all the elements got scanned successfully.始终检查sscanf()的返回值以确保所有元素都被成功扫描。
  3. Change stampa( struct Cerchi *bufcer);改变stampa( struct Cerchi *bufcer); to stampa(bufcer);stampa(bufcer); This is a function call not a function definition or declaration.这是一个函数调用,而不是一个函数定义或声明。 Also, add struct Cerchi *bufcer = NULL;另外,添加struct Cerchi *bufcer = NULL; inside main() before calling stampa() .在调用stampa()之前在main()内部。
  4. You are not using the return value of leggi_file () .您没有使用leggi_file ()的返回值。 As per your logic, you need to collect the value in bufcer .根据您的逻辑,您需要收集bufcer的值。 Change your code to将您的代码更改为

    bufcer = leggi_file(fp);
  5. Always check for the success of malloc() before using the returned pointer.在使用返回的指针之前总是检查malloc()是否成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM