Hi all,
Just had q query wrt to the string "0x00" used in memset of character arrays in C. Could anyone reply on this!!
Thanks,
Vandrea
Hi all,
Just had q query wrt to the string "0x00" used in memset of character arrays in C. Could anyone reply on this!!
Thanks,
Vandrea
I don't understand your question. You don't pass a string to memset. If you're talking about the integral constant 0x00, it's just the hex encoding of, well, 0.
yep u were right..i meant of the integral constant!
Consider the follg eg:
So after the above memset i would have following values in my stringCode:char abc_ca[3]; memset(abc_ca,0x00,size(abc_ca));
where {'\0'} identifies the end of string...Just wanted to know if my understanding is right!Code:abc[0]={'\0'} abc[1]={'\0'} abc[2]={'\0'}
Yep. On a side note, though, just keep in mind that sizeof used in that way won't work on an array passed to a function or on a pointer to an array.
Thanks Sebastiani for the quick reply...but dint get as to why cant we use sizeof in memset ?
Last edited by vandrea; 08-16-2009 at 04:22 AM. Reason: error
Well, for example:
And when you pass an array to a function, it resolves to a pointer, so the effect is the same as in the second print statement.Code:int main( void ) { char buf[ 1024 ], * ptr = buf; printf("sizeof( buf ): %d\n", sizeof( buf ) ); printf("sizeof( ptr ): %d\n", sizeof( ptr ) ); return 0; }
I understood ur example but still wondering as to y cant we use sizeof in the above memset sinceWell, for example:
And when you pass an array to a function, it resolves to a pointer, so the effect is the same as in the second print statement.Code:Code: int main( void ) { char buf[ 1024 ], * ptr = buf; printf("sizeof( buf ): %d\n", sizeof( buf ) ); printf("sizeof( ptr ): %d\n", sizeof( ptr ) ); return 0; }wuld return 3 I guess..Code:sizeof(abc_ca)
>> I understood ur example but still wondering as to y cant we use sizeof in the above memset since
The point is simply that you can use it on 'buf' but not 'ptr'. And inside a function, you'd have the same problem:
Code:void test( char buf[ 1024 ] ) { printf("sizeof( buf ): %d\n", sizeof( buf ) ); }
Thanks Sebastiani...Now I get it!