开发者

cannot fscanf into a double dereferenced pointer

开发者 https://www.devze.com 2023-01-28 17:44 出处:网络
I have an array elsewhere in my program: data=mal开发者_运维百科loc(sizeof(int)*lines); I want to read data from a file into this array. I have opened the file etc.

I have an array elsewhere in my program:

data=mal开发者_运维百科loc(sizeof(int)*lines);

I want to read data from a file into this array. I have opened the file etc.

I created a function to read the data into this array:

int readfile(FILE* fp,int** storage_array,int lines)

{
    int i=0;

    for(i=0; i<lines; i++)
    {
        fscanf(fp,"%lf",&(**storage_array+i));
    }


    rewind(fp);

    return 0;

}

Dev c++ gives me

invalid lvalue in unary '&'

I have tried so many different ways to get this to work and it's really stressing me out :(

Have you guyd got any ideas what i'm doing wrong?

Thanks so much :)


In C, a pointer-to-int (int *) variable holds the address of an integer, and can also be used for holding the address of the 1st integer in an array of several because you can reach the 2nd and subsequent integers just by adding to the address of the 1st. malloc() gives you the address of a block of memory which you will use to hold lines consecutive integers, so you should be storing the address it gives you in a pointer-to-int variable, and the relevant readfile() parameter should have this type too.

Inside readfile(), you want to give the fscanf() call the address of the ith integer. You can get this by simply adding i to the original address, because C takes care of multiplying i by sizeof (int) for you:

int *data;
data=malloc(sizeof(int)*lines);
readfile(fp, data, lines);

...

free(data);    /* Don't forget to release the memory eventually */

...

int readfile(FILE* fp,int* storage_array,int lines)
{
    int i=0;

    for(i=0; i<lines; i++)
    {
        fscanf(fp,"%lf",storage_array+i);
    }


    rewind(fp);

    return 0;

}

The fscanf() line could be equivalently written

fscanf(fp,"%lf",&(*(storage_array+i)));

or even

fscanf(fp,"%lf",&storage_array[i]);

since in C, the expressions *(a + b) and a[b] are equivalent in every way.


You should look very closely at the way you're using the pointers and its showing in your question...

  1. The usage of your function returning 0 if its reading the data into an array - why return 0? Its redundant, make that void function....
  2. Your lack of understanding pointers is obviously weak... you are using a double pointer variable, in which it is passed by reference, so you used a double de-reference which is incorrect - that should be a single de-reference, as the other asterisk indicates an address-of that variable...
  3. If this is what's tripping you up, why not do it this way, read into a separate variable and stuff the variable into the by-reference parameter of array instead to make it easier to avoid the complexity and confusing the issue with pointers

See below ... error checking is omitted....

void readfile(FILE* fp,int** storage_array,int lines)
{
    int i=0;

    for(i=0; i<lines; i++)
    {
        int readval = 0;        
        fscanf(fp,"%d",&readval);
        *(storage_array[i]) = readval;
    }
    rewind(fp);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消