Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C passing argument 1 of ‘fread’ makes pointer from integer without a cast

Tags:

c

I'm sorry to bother you with this, but i'm stuck with it for too long already. I get the following warning on fread: "warning: passing argument 1 of ‘fread’ makes pointer from integer without a cast"
I'm new to C and really like it, but don't get over this.
Thanks in advance.

    typedef unsigned char byte;

    int main( int argc, char *argv[]){
        FILE * filein;
        filein = fopen(argv[1], "r" );
        int width=10;
        int height=10;
        byte ** data;

        // Allocation
        data = (byte **) malloc(height*sizeof(byte *));
        for(int i=0;i<height;i++){
            data[i]=(byte *) malloc(width*sizeof(byte));
        }

        for(int i=0;i<height;i++){
            for(int j=0;j<width;j++){
                fread(data[i][j], sizeof(const char), 1, infile);
            }
        }

        for(int i=0;i<height;i++){
            free(data[i]);
        }
        free(data);
        fclose(filein);

        return 0;
        exit(0);
    }

This is only a small piece of the actual program. The task is to read a binary pgm-image, store it in a data-matrix, normalize the values and write them to a new binary pgm-image.

like image 232
Ed Vayne Avatar asked Dec 11 '25 13:12

Ed Vayne


2 Answers

fread() expects the void* pointer to the buffer. You are passing the value stored in data[i][j], not the pointer to the element data[i][j].

Try using the

fread(&data[i][j], sizeof(const char), 1, infile);

By the way, .pgm file format (if this is it) has a header and it is not sufficient to read only the width*height characters. The pixel values are also separated by spaces, so a little parsing is required. Also keep in mind that the end-of-line symbols also take space (.pgm cannot have more than 70 characters on one line)

like image 181
Viktor Latypov Avatar answered Dec 13 '25 01:12

Viktor Latypov


The type of data[i][j] is byte. That is not a pointer. What you really want is to read to &data[i][j], if you're reading one byte at a time.

like image 39
Steve Howard Avatar answered Dec 13 '25 02:12

Steve Howard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!