0

I have gone through fread function where syntax is like when I gone through some examples for fread I found the below code.

I understood that in the fread function first parameter is address of block of memory to store the objects, second parameter is size of each object in bytes, third parameter is no of objects, final parameter is pointer to the file.

Syntax:

size_t fread(void * buffer, size_t size, size_t count, FILE * stream).

Code:

int a_var= 0;
FILE *file;
fread(&(a_var),4,1,file);

I don't understood why a_var is used there. Does it indicate address for value 0 or address 0?

RAP
  • 137
  • 2
  • 12

4 Answers4

2

With the address-of operator & you get the address of something, i.e. you get a pointer to something.

So with &PixelDataOffset you get a pointer to the variable PixelDataOffset.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

The fread function needs a address for the buffer as first parameter. So you need the & operator to get the address of the variable PixelDataOffset to get a address to the write data for the file. If you write

fread(PixelDataOffset,4,1,fp);

the function will assume that the value of PixelDataOffset is a address and will take data from address 0. Another solution is

int Data = 0;
int* DataPtr = &Data;
fread(DataPtr,4,1,fp);
Kampi
  • 1,798
  • 18
  • 26
0

Depending on where int PixelDataOffset = 0; is written , PixelDataOffset will be stored either at stack or data segments of your primary memory.

The contents of that location will be 0.

Now &PixelDataOffset will returns the address of location where PixelDataOffset is stored.

So to answer your question precisely : it indicate address for value 0 .

Vagish
  • 2,520
  • 19
  • 32
0
int PixelDataOffset = 0;

In your code PixelDataOffset is a variable of datatype int.

This variable has some address (Let us say address is 0x1000)

Value assigned to this address at the time of declaration is '0'.

Implies at address 0x1000 value '0' is stored.

Does it indicate address for value 0 or address 0?

argument passed to fread is &(PixelDataOffset)

Implies address(0x1000) of variable PixelDataOffset is passed.

I don't understood why PixelDataOffset is used there.

Answer is simple, fread first argument is some address.

In this example address of PixelDataOffset is passed

Babajan
  • 364
  • 3
  • 5