W
W
WalterBlack2020-11-02 13:14:36
C++ / C#
WalterBlack, 2020-11-02 13:14:36

Passing argument 1 of 'fwrite' makes pointer from integer without a cast?

Hello. There is an integer type variable with value 7. I am trying to write this variable to a text file with the fwrite() function;

Here is the code:

int size = 7;
FILE *fp;

fp = fopen("saves.txt", "w");
fwrite(size, 1, sizeof(size), fp);
fclose(fp);


But the compiler throws this error:
warning: passing argument 1 of 'fwrite' makes pointer from integer without a cast

What could be the problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2020-11-02
@Rsa97

That you didn't bother to read the description of the fwrite function .
The first parameter must be a pointer to the data being written, the second is the size of a single data element, and the third is the number of these elements.

V
Vladimir T, 2020-11-02
@32bit_me

The fwrite() function writes count objects—each object is size characters long—to the stream specified by stream from the character array specified by buf. That is, instead of size, you must pass a pointer to the buffer, from where fwrite will copy the characters to the file.

C
CityCat4, 2020-11-02
@CityCat4

man fwrite
You are trying to read data from area 0x7, which is of course wrong. It is necessary to transfer the pointer to the area as the first parameter. And in general, you have some kind of porridge in the parameters.
fwrite(&size, sizeof(size), 1, fp);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question