Null Terminator


Home

About the Author

General Programming

Pascal

Delphi

C&C++

Visual Basic

SQL

JAVA Script

Links

 

Assert vs Verify ] Constructors and Destructors ] Falling through switch statements ] Memory Leaks ] [ Null Terminator ] Rogue array pointers ]

When using a C-style string (a char array), don't forget to include the null terminator, which is the zero character added to a char array indicating that it has reached its end.

Here's an example:

char Name[4] = {'J','o','h','n'};

Although there are four chars in the Name array and there are four letter in the name "John," this is incorrect because the string wasn't terminated.

Here's the correct way:

char Name[5] = {'J','o','h','n','0'};

Now the last letter is zero and terminate the string. This way, when function like strcat or strcpy look at this car array, it will know when the string ends and when it should stop reading it.

An alternative to this is to use the STL's string class:

string = "John";

Link of the week 
http://www.cpp-
programming.
com/
Newsgroups
comp.lang.c
comp.lang.c++
comp.programming

This page is best viewed in 800x600x256. This page was last edited Saturday, July 15, 2000