Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty character constant in c++

Tags:

c++

I copied this code from a tutorial to play around with, however I kept on getting an error that stated that I can't have any empty character constants. the tutorial was in VS 2008, and I am using VS 2013, so perhaps this is no longer valid, but I can't find any fix. here is the code:

#include "stdafx.h"
#include <iostream>

class MyString
{
private:
    char *m_pchString;
    int m_nLength;

public:
MyString(const char *pchString="")
{
    // Find the length of the string
    // Plus one character for a terminator
    m_nLength = strlen(pchString) + 1;

    // Allocate a buffer equal to this length
    m_pchString = new char[m_nLength];

    // Copy the parameter into our internal buffer
    strncpy(m_pchString, pchString, m_nLength);

    // Make sure the string is terminated
    //this is where the error occurs
    m_pchString[m_nLength-1] = '';
}

~MyString() // destructor
{
    // We need to deallocate our buffer
    delete[] m_pchString;

    // Set m_pchString to null just in case
    m_pchString = 0;
}

    char* GetString() { return m_pchString; }
    int GetLength() { return m_nLength; }
};

int main()
{
    MyString cMyName("Alex");
    std::cout << "My name is: " << cMyName.GetString() << std::endl;
    return 0;
} 

The error I get is the following:

Error   1   error C2137: empty character constant       

Any help will be greatly appreciated

Thanks again.

like image 330
BeepBoop Avatar asked Jul 02 '15 19:07

BeepBoop


People also ask

What is an empty character in C?

There is no such thing as the "empty character" '' . If you need a space character, that can be represented as a space: c[i] = ' ' or as its ASCII octal equivalent: c[i] = '\040' . If you need a NUL character that's c[i] = '\0' . Follow this answer to receive notifications.

What is character constant in C?

A "character constant" is formed by enclosing a single character from the representable character set within single quotation marks (' '). Character constants are used to represent characters in the execution character set.

What is the char value for empty string?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.

Can you have an empty char?

An empty char value does not belong to any char, so Java gives a compile-time error. To create an empty char, we either can assign it a null value \0 or default Unicode value \u0000 .


2 Answers

This line:

m_pchString[m_nLength-1] = '';

What you probably mean is:

m_pchString[m_nLength-1] = '\0';

Or even:

m_pchString[m_nLength-1] = 0;

Strings are zero terminated, which is written as a plain 0 or the null character '\0'. For double quote strings "" the zero termintation character is implicitly added to the end, but since you explicitly set a single character you must specify which.

like image 169
Tommy Andersen Avatar answered Sep 20 '22 15:09

Tommy Andersen


what do you think about null-terminated string? Yes, you are right, such strings must be terminated with null:

m_pchString[m_nLength-1] = 0;

like image 45
AnatolyS Avatar answered Sep 20 '22 15:09

AnatolyS