I am trying to assign the min value of a char to a variable of type char array like this
char charchar_value[MAX_VARCHAR_LENGTH + 1]= CHAR_MIN;
it gives me this error “incompatible types in assignment of ‘char’ to ‘char [513]’”
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Initialization of array object requires the use of brace initializer list, i.e. something along the lines of
This would initialize the first
nvalues to their respective values and all others to zero. This isn’t quite what you want. The only way to set the values in the array is after its initialization, using e.g.std::fill():Since there is another suggestion recommending the using essentially a C approach:
memset()is limited to types without any C++ features (as is, indeed the case forchar) and when setting the value to something other than0it is pretty much limited tochar(yes, I realized that there are cases where it would still work).std::fill()on the other hand, works with any type which is CopyAssignable and with any value given to an object of the corresponding type.sizeof()work in this case, it won’t work the moment the size becomes dynamic but the compiler would still happily accept the code. Getting thestd::begin()orstd::end()of a pointer would fail.std::numeric_limits<T>::min()has a better chance to work. It still won’t work for many type as there is notmin()defined but the scope is bigger.All that said, I would just use an appropriate class instead which does the initialization directly, i.e.
This allocate the memory dynamically, though, i.e. it isn’t entirely equivalent. However, unless there is a good reason to assume otherwise, I would use the simple approach and only optimize if it is necessary.