Click to See Complete Forum and Search --> : How to assign null value to a char array
SunnyPriya
June 19th, 2002, 02:54 AM
Hi
I have a character array, say
char name[10] ;
I want to assign null value to it.
Can I do something like
name = null ;
or
strcpy(name,null) ;
Help is needed.
Regards
SunnyPriya Joshi
:)
WildCat
June 19th, 2002, 03:05 AM
memset(name, 0, sizeof(char) * 10)
Elrond
June 19th, 2002, 04:49 AM
2 possibilities:
If you want to use it as a string, then, set the fisrt character to null should be the best:
name[0] = '\0';
If you want to put all the elements of your array to nul, then use memset as WildCat says.
JMS
June 19th, 2002, 12:51 PM
Null is a pointer value. So you could say
char *pszMyString = Null;
and thus set a pointer to NULL but not set a string to NULL.
Now the termination character for a string is also called the NULL terminator but it is something different than NULL which as I said is a pointer value. The NULL termination character for a string is the ascii value absolute Zero... [ 0 ]. or '\0'.
One can set the first character of a character array to this value which effectively ends the string at element Zero.. Like this..
char szMyString[5] = "abcd"
szMyString[0] = '\0';
Now this doesn't exactly overwrite the entire array. Anybody trying to access this array as a string will just think it ends at element 0. The contents of the szMyString array looks like this..
0 123 4
'\0bcd\0'
To overwrite the entire array with the Null terminator use memset as has already been discussed..
memset ( (void *) &szMystring, '\0', sizeof(szMystring));
this will make the array contents
0 1 2 3 4
'\0\0\0\0\0'
get it?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.