+ 1

Why im getting an error?

int main () { char *some[]={ "word", "another", "qwerty" }; the error is: cant make const * char for char variable (sorry for bad english ) btw it compiles perfectly if i do this in online compiler

8th Dec 2021, 9:56 AM
Artur
Artur - avatar
10 Respostas
+ 7
Because "word", "another", and "qwerty" are all string litterals You can only read them, but you cannot write, so they have type 'const char[N]' But you're pointing to them without the const qualifier, which implies you could modify them in the future To solve this just put 'const' before char
8th Dec 2021, 10:12 AM
Angelo
Angelo - avatar
+ 4
Define it as const string array ... const char* some[] = { ... };
8th Dec 2021, 10:03 AM
Ipang
+ 3
try closing the main function with a bracket.
8th Dec 2021, 10:03 AM
Kruszyn
+ 2
nvm i got it. Thank you guys!
8th Dec 2021, 6:27 PM
Artur
Artur - avatar
+ 2
Artur, In Code Playground, I only get such warning about const char* when I pasted (and ran) the snippet into a C++ code, there's no warning if I pasted the code into a C code. The warning given by C++ compiler mentioned about C++ ISO standard, so it seems to be a C++ standard, not adopted in C standard. Dunno about VS 2019, or why it differs from your installation and your friend's ...
8th Dec 2021, 6:30 PM
Ipang
+ 1
Visual studio 2019 failed it, however the online version of C ( on this site ) allowed it. also i tried this code on my friends pc ( VS2019 as well ) and it worked.
8th Dec 2021, 6:05 PM
Artur
Artur - avatar
0
Angelo, i may be wrong, but if i put "const" i cant redact this array later?
8th Dec 2021, 6:05 PM
Artur
Artur - avatar
- 1
sorry i can't understand... yet, so sad😥
8th Dec 2021, 6:31 PM
RTB
RTB - avatar