How to initialize a char array with double quotes (not printable escape characters) in C++

Derek Todd 0 Reputation points
2023-02-03T19:31:53.7533333+00:00
Hi,



I am trying to initialize a character array with double quotes inside of the array.  I am never going to print the array out, so I do not need the printable escape characcter \".  I want to be able to read and compare the double quote inside of the array in the program.  Is there a way to do this.  The char array is very large so initializing the char array like this will take up to much time:



char hello[6] = { 'h', 'e', 'l', 'l', 'o', '/0' };



Here is a small portion of the character array I am trying to initialize:

	char BankgothicFont[] = {

"char id = "39" x = "252" y = "70" width = "3" height = "7" xoffset = "1" yoffset = "2" >"

"<char id = "40" x = "58" y = "49" width = "6" height = "12" xoffset = "2" yoffset = "5" >"

"<char id = "41" x = "65" y = "48" width = "6" height = "12" xoffset = "1" yoffset = "5" >"

"<char id = "42" x = "94" y = "109" width = "8" height = "8" xoffset = "2" yoffset = "6" >"

"<char id = "43" x = "48" y = "35" width = "13" height = "13" xoffset = "2" yoffset = "4" >"

"<char id = "44" x = "181" y = "105" width = "5" height = "7" xoffset = "2" yoffset = "12" >"

};



I have already tried using the \" escape character, but it is stored in the array as \".  I want to just store the double quote char ".  Is there a way to do this?  Thanks for your help.



D
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,643 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,544 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-02-03T20:02:52.65+00:00

    It doesn't have anything to do with printable or non-printable. \ is escape character that you need to use to escape " in a string.

    Both declarations are valid and do the same thing:

    #include <iostream>
    
    int main()
    {
        char Hi1[] = { '"' , 'H', 'i', '"' , '\0' };
        char Hi2[] = { "\"Hi\"" };
    
        std::cout << Hi1;
        std::cout << Hi2;
    }
    
    

    For more information, take a look at String and character literals.


  2. WayneAKing 4,921 Reputation points
    2023-02-04T00:57:25.5066667+00:00

    I have already tried using the " escape character, but it is stored in the array as "

    How did you determine that the backslash is stored in the array?

    If you are looking at the array in the IDE debugger then you need to be aware that it will add the backslash to the display when showing escaped characters. However if you look at each element in the array you will see that only a quote character (dec 34, hex 22) is at that location in the array.

    char arry[] = "123\"456";
    
    

    double quote in array

    • Wayne
    0 comments No comments