Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sizeof operator

Status
Not open for further replies.

seanken

Programmer
May 2, 2001
24
0
0
US
Hi,

Can anyone tell me why the second printf returns the sizeof(record) as 4 whereas the first returns 10 as I would expect?

Thanks

#include <stdio.h>
#include <string.h>

void read_input(FILE *ifile, char *record);

main(){

FILE *ifile;
char record[10+1];

if((ifile=fopen(&quot;input.dat&quot;,&quot;r&quot;))==NULL)
{
printf(&quot;Input file not found\n&quot;);
}

printf(&quot;record length is %d\n&quot;, sizeof(record));
read_input(ifile, record);

}

void read_input(FILE *ifile, char *record){

fread(record,1,sizeof(record), ifile);

printf(&quot;record length is %d\n&quot;, sizeof(record));

}
 
First you should be getting 11 & 4, not 10 & 4, as the sizes of your records.

You have two different data types:
char record[10+1] // an array of 11 char's
char *record // a pointer to char's

sizeof() returns the amount of memeory that is used to store each.
The array physically takes 11 bytes, the sizeof() the pointer is only 4 bytes.
 
Sorry, meant 11 but typed 10. Thanks for the explaination. Makes sense when someone shows you how.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top