-3

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

    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] Which error I'm doing? Can you try to solve it and help me? Thanks a lot...

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    `bufcer=(cerchio *)malloc(sizeof (int)*10000000);` Why `sizeof(int)`? Shouldn't that be `sizeof(cerchio)`? – PaulMcKenzie Jun 16 '15 at 17:40
  • 1
    Please **learn** the basics of the language before trying to write programs in it. You wouldn't try to speak e. g. Russian if you have never learnt it, would you? – The Paramagnetic Croissant Jun 16 '15 at 17:42
  • 1
    You malloc a `buffer` in `leggi_file`, return it, forget to store it in `main`, and expect to print it in `stampa`. How is this supposed to work? – byako Jun 16 '15 at 17:49
  • I have to do Realloc function because there are 22 Line obtained (And 22 data in struct too) from a txt block , btw the first problem was that i can't pass the struct , so realloc become My problem and i don't want to ask to anyone something about that and i solved like that >.< Byako ; I malloc a vector of struct in the function – AncientSniper94 Jun 16 '15 at 17:51
  • 1
    You have ignored the return value from `*leggi_file()`. But right after you try to use it with `stampa( struct Cerchi *bufcer);` which is trying to use a variable which is neither accessible, or still in scope. Oops @byako you already said it. – Weather Vane Jun 16 '15 at 18:06

2 Answers2

0

You have to use -> operator when accessing structure elements, so your printf will be as

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

Also, -> operator use for scanf()

You need to pass & of structure to pass structure pointer

Amol Saindane
  • 1,568
  • 10
  • 19
  • programma2.c:18:76: error: invalid type argument of ‘->’ programma2.c: In function ‘main’: programma2.c:50:10: error: expected expression before ‘struct’ stampa( struct Cerchi *bufcer); – AncientSniper94 Jun 16 '15 at 17:54
  • 1
    How you are passing structure pointer ? You need to pass address of structure. Please check below link to understand structure pointers : https://fresh2refresh.com/c/c-passing-struct-to-function/ – Amol Saindane Jun 16 '15 at 17:57
0
  1. Do not cast return value of malloc().
  2. Always check the return value of sscanf() to ensure all the elements got scanned successfully.
  3. Change stampa( struct Cerchi *bufcer); to stampa(bufcer); This is a function call not a function definition or declaration. Also, add struct Cerchi *bufcer = NULL; inside main() before calling stampa().
  4. You are not using the return value of leggi_file (). As per your logic, you need to collect the value in bufcer. Change your code to

    bufcer = leggi_file(fp);
    
  5. Always check for the success of malloc() before using the returned pointer.
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • The return of sscanf is correct ; Now i have this error : programma2.c: In function ‘main’: programma2.c:50:9: error: ‘bufcer’ undeclared (first use in this function) stampa(bufcer); How to take it from void return? – AncientSniper94 Jun 16 '15 at 18:17
  • *please* read our comments. You need to store the value returned by `leggi_file` when you call it in `main`: the variable `bufcer` in `leggi_file` is out of scope in main. Try `struct Cerchi *bufcer = leggi_file();`, but you should really read a C tutorial. – byako Jun 16 '15 at 18:31
  • @byako Thank you, most valuable information. added that in my answer itself. :-) – Sourav Ghosh Jun 16 '15 at 18:44
  • @SouravGhosh i read at all , if you look at updated code :\ I make everything u say me to do i think ... You mean to remove Void before stampa(bufcer) ? – AncientSniper94 Jun 16 '15 at 19:56