#include <iostream>
#include <string>
// Avoid defines in C++:
constchar VAL1 = 1;
constchar VAL151 = 151; // if char is signed, this will result in a warning that can be safely ignored.
int main()
{
std::string str = { VAL1, VAL151 };
std::cout << str;
}
@cire this is true , but prefer using unsigned and signed , it is less confusing about the range and tells exactly what we are looking for. you can also used type like __int8 or unsigned __int8 . (char -> 8 bits <=> 1 byte , always (except maybe in c# ...))
@cire this is true , but prefer using unsigned and signed , it is less confusing about the range and tells exactly what we are looking for.
In this case it would appear we're looking for the type c-strings are made of (which is char,) so it's less confusing to use exactly what we're looking for which is neither unsigned char nor signed char. ;)
I plan to use this construction as an element of the structure:
struct example {
std::string str1; // works OK
// unsigned char str2[]; // doesn't work, "zero-sized array in struct/union"
};
struct example example1[2] =
{
{ { 1, 2, 0 } },
{ { 1, 2, 3, 0 } }
};
Each array element will be initialized with a different numbers of values
std::string solution works fine
I am just curious, is it possible to declare and initialize just a char string in the same way without any classes like std::string?
Actually what I need is just a NULL-terminated string declared in a strange way