cwshore
cwshore

Reputation: 23

Null terminator \1?

I am doing some coding for a beginner C++ class I am taking. In the class, we have to take code submitted by another student and fix a bug they created. The code is as follows:

#include <iostream>
using namespace std;

int countChars(char *, char);  // Function prototype

int main()
{
    const int SIZE = 51;    // Array size
    char userString[SIZE];  // To hold a string
    char letter;            // The character to count

    // Get a string from the user.
    cout << "Enter a string (up to 50 characters): ";
    cin.getline(userString, SIZE);

    // Get a character to count occurrences of within the string.
    cout << "Enter a character and I will tell you how many\n";
    cout << "times it appears in the string: ";
    cin >> letter;

    // Display the number of times the character appears.
    cout << letter << " appears ";
    cout << countChars(userString, letter) << " times.\n";
    return 0;
}

int countChars(char *strPtr, char ch)
{
    int times = 0;  // Number of times ch appears in the string

    // Step through the string counting occurrences of ch.
    while (*strPtr != '\0')// ***** There was a one placed inside the null operator, however, this is not a syntax error, but rather just incorrect.
    {
        if (*strPtr == ch)  // If the current character equals ch...
            times++;         // ... increment the counter
        strPtr++;           // Go to the next char in the string.
    }

    return times;
}

The student changed the function such that it had the null terminator as \10, which did not cause a compile nor run time error. After playing with it, I found that it could also be \1 and still work. How is this possible. I am a complete noob, so I apologize if this is a stupid question, but I assumed that this was a boolean operator and 1 was true and 0 was false. The question is why will \10 and \1 work as the null terminator. Thank you in advance!

Upvotes: 2

Views: 964

Answers (2)

BusyProgrammer
BusyProgrammer

Reputation: 2781

The "\0" is the only one to be called a NULL terminator. Even though "\1" or "10" or even "\103" work, only "\0" is referred to as the NULL terminator. I'll explain why.

In "\0" the 0 refers to the OCT value in the ascii table (see picture below). In the ascii table, there is no character whose OCT value is 0, therefore, if we try to use 0, it is referred to as a NULL terminator, because it points to the ground and nothing meaningful or useful.

Now, why does "\10" and "\1" work? Because these refer to OCT values 1 and 10, which can be mapped to a character on the ascii table, notably Start of Heading and Baskspace. Similarily, if you pick a OCT value that points to a letter, punctuation mark or a number, like for example, "\102" points to B, it will output B.

If you try an invalid number, like "\8", then it will simply output 8 on the screen, since it does not refer to a valid character on the ascii table.

See this code, it summarizes all the different types of pointers:

#include <iostream>

using namespace std;

int main(void)
{
    cout << "\0" << endl; // This is NULL; points to the ground
    cout << "\8"; << endl; // Invalid OCT value; outputs invalid number input with only a warning. No compilation error. Here, 8 will be output
    cout << "\102"; // Valid; points to B. Any valid OCT value will point to a character, except NULL.
}

Ascii Table


EDIT: After doing some research, I noticed that the correct way to use the escape sequences is indeed with only 3 numbers at minimum. Therefore, even the NULL terminator should technically be written as "\000", following the octal method. However, the compilers can apparently tell which octal value is being referred to even if it is not written in octal format. In other words, "\1" is interpreted by the compiler as "\001". Just some extra information.

I found this information at: C++ Character Literals

Upvotes: -1

John Zwinck
John Zwinck

Reputation: 249542

'\0' means "the character having the integer representation 0." Similarly, '\10' means "the character having the integer representation 10." That's why it's not a compilation error--only a logical error.

Upvotes: 4

Related Questions