I can think of three rules for concocting your own string in the C language.
♦ First, the string needs a location in memory.
♦ Second, the string has to have a given size. Going along with that is typing the size to store char variables.
♦ Finally, the string must have an end.
The string’s size and its end may seem like the same concepts, but in the C language they’re not. When you assign storage space for a string, you’re allocating a given chunk of memory. The string may or may not occupy that entire space, which is why the string must have a definite stopping point.
I’ve coded my own string functions in C to handle input or manipulate string constants. The C library function I use most often is strcat(), which glues two strings together.
Here’s a chunk of code from a text mode game I wrote. It creates a hidden file in the user’s home directory to store the high scores table:
strcat(HighScoreFile,homedirprefix);
strcat(HighScoreFile,"/.");
strcat(HighScoreFile,HIGH_SCORE_FILENAME);
The first line copies the user’s home directory prefix (obtained from the operating system’s environment table) into a string buffer, HighScoreFile. The second line appends the string constant /. to that buffer. Finally the third line appends the high score filename, referenced by the constant HIGH_SCORE_FILENAME, to the buffer. At this point, the string stored in the buffer HighScoreFile contains the full pathname to the high score file.
The original string buffer, HighScoreFile, is a 256-byte chunk of memory designated as a char buffer. Such storage is necessary when you build strings from scratch. The following code shows an example, carefully following the three rules mentioned at the start of this Lesson.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *buffer,*b;
/* Create a 4-character string buffer */
buffer = (char *)malloc(5 * sizeof(char));
if(buffer == NULL)
{
puts("Unable to allocate buffer");
exit(1);
}
/* Create the string */
b = buffer;
*b++ = 'A';
*b++ = 'B';
*b++ = 'C';
*b++ = 'D';
*b = '\0';
/* Display the string */
printf("The string \"%s\" was created\n",buffer);
return(0);
}
Here’s how the code works with regards to my three rules:
♦ First, the string needs a location in memory.
This job is handled by the buffer pointer created at Line 6. The pointer is empty, of course, but it’s designed to reference the starting address of an array of char variables — a string.
♦ Second, the string has to have a given size. Going along with that is typing the size to store char variables.
This job is handled by the malloc() function at Line 9. Storage space for 5 char variables is created. That’s four characters for the string plus one for the null character, \0. The storage space is typecast to hold char variables. That all happens in Line 9.
♦ Finally, the string must have an end.
The string is created starting at Line 17. Pointer b is initialized to pointer buffer‘s address, the start of string storage. Letters A through D are stored in the string; the *b++ construction stores the character and then increments pointer b to reference the next location in the buffer.
Finally, at Line 22, the null character is stored at the end of the string. Variable b doesn’t need to be incremented at that point as it already references the fifth position in the buffer.
Line 25 displays the result.
The string "ABCD" was created
I’ve added Figure 1 below to help illustrate how the string is populated. Remember: The key is to cap any string with a null character, \0. If you use the C library string functions, such as strcat(), then the null character is dealt with automatically. Do it yourself, however, and you must remember the \0.

Figure 1. How pointer b creates the string in memory.