[En-Nut-Discussion] pointer don't work?

Bernard Fouché bernard.fouche at kuantic.com
Thu May 12 16:26:54 CEST 2011


Le 12/05/2011 15:28, Landsperger, Werner a écrit :
> char *buff;
> buff = malloc(128);
> char *pointer;
>
> cnt = fread(buff, 1, 128, istream);    // read in a buffer ->  cnt = 128
> pointer =&buff;                               // should set a pointer
> to buff
> cnt = strlen(pointer);                        // should be 128 but ->
> cnt = 22 ????
>
I'm surprised that the compiler does not complain about 'pointer=&buff' 
: 'pointer' should be 'char **' to use it that way. That's why it does 
not work: you set 'pointer' to the adress of 'buff', not to the value of 
'buff'. You should have 'pointer=buff'. And as Stefan Hax wrote, you 
must also be sure that what you got from fread() is null terminated to 
have strlen() to work correctly.

     Bernard



More information about the En-Nut-Discussion mailing list