0

I have problem with my program I don't know what to do :(

double spocitaj(FILE *fr,double **polsum, int *cena){

int r=0, i=0,k=0;
double n=0;
char c;


while((c=getc(fr))!=EOF){
    if(c=='\n') r++;
    if(r==4){
        *cena=k++;
        r=5;
    }
    if(r==6) r=0;       
}

*polsum=(double *)calloc(k,sizeof(double));
r=1;
rewind(fr);

while((c=getc(fr))!=EOF){

    if(c=='\n') r++;
    if(r==4) {
                    ungetc(c,fr);
            fscanf(fr,"%lf", &n);
            *polsum[i]=n;
            i++;
    }
    if(r==6) r=1;       
}

for(i=0;i<*cena;i++)
                    printf("%.2lf\n", *polsum[i]);
return 0;

}

Can you help me please? this is a message: Unhandled exception at 0x012947F8 in Projekt 1.exe: 0xC0000005:

Access violation writing location 0xCCCCCCCC.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Tomáš Čičman
  • 281
  • 1
  • 5
  • 17

1 Answers1

5

*polsum[i]=n; should be (*polsum)[i] = n;.

*polsum[i] is *(polsum[i]), which treats polsum as an array, but you likely intend it to be a single pointer, a pointer to where there is a double *.

Changing this to (*polsum)[i]) says "Look up the double * that is where polsum points. That double * points to a place where there are many double objects. Get the ith one.”

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312