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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't this work?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Can someone give me the official reason this doesn't work in C using Visual C++. It works in C in Unix environments but crashes in a simple VC++ console application. The answer should be obvious but I don't see it.

char somevar[20];

strcpy( somevar, (char *)NULL);

Thanks in advance.
 
Because strcpy() expects a const char * for its 2nd argument, not NULL.

The C standard doesn't define what happens when you pass strcpy() a NULL argument, so a crash is quite appropriate.

If you're trying to initialize somevar to an "empty string," do this:

char somevar[20]={0};

This has the effect of setting all the bytes in somevar to '\0'.

Only pointers should be assigned NULL anyway, which is totally different from the null character '\0' that terminates strings.

Russ
bobbitts@hotmail.com
 
Thanks, Russ. I suspected something along those lines but wanted to hear it from an expert.

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top