![]() |
|
[C] const char *" cannot be assigned to an entity of type - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] const char *" cannot be assigned to an entity of type (/showthread.php?tid=5833720) Pages: Pages
1
2
|
const char *" cannot be assigned to an entity of type - maryous - May 6, 2018 Hey I was watching a tutorial and i got stuck with this issue a value of type "const char *" cannot be assigned to an entity of type "char *" '=': cannot convert from 'const char [9]' to 'char *' I tried to look up the issue and couldn't figure out how to fix it I know what the issue is but i don't know how to fix it this's where im getting the issue and if needed i can post the entire code Code char *arr_name;Code if (argc >= 3) {RE: const char *" cannot be assigned to an entity of type - Annabelle Lee - May 6, 2018 This: arr_name = "filedata"; should be: arr_name = 'filedata'; I believe that'll solve your issue. RE: const char *" cannot be assigned to an entity of type - Hash G. - May 6, 2018 (May 6, 2018 04:04 PM)Annabelle Lee Wrote: This: arr_name = "filedata"; should be: arr_name = 'filedata'; I believe that'll solve your issue. Absolutely not. A string must be surrounded by double quotes. Single quotes means a single character (and thus a char and not a char array). @OP: From what I undertstands your code looks like this: Code #include <stdio.h>Which is a totally legal C code (and compiles perfectly fine). Please post your whole code. RE: const char *" cannot be assigned to an entity of type - maryous - May 6, 2018 (May 6, 2018 04:04 PM)Annabelle Lee Wrote: This: arr_name = "filedata"; should be: arr_name = 'filedata'; I believe that'll solve your issue. I already tried this and when i change it i get this error "too many characters in character constant" "too many characters in constant" RE: const char *" cannot be assigned to an entity of type - Annabelle Lee - May 6, 2018 (May 6, 2018 05:45 PM)Hash G. Wrote: Absolutely not. A string must be surrounded by double quotes. Single quotes means a single character (and thus a char and not a char array). Oh I see where I went wrong, not sure why I assumed it the way I did. Thanks for catching that. RE: const char *" cannot be assigned to an entity of type - maryous - May 6, 2018 (May 6, 2018 05:45 PM)Hash G. Wrote: Absolutely not. A string must be surrounded by double quotes. Single quotes means a single character (and thus a char and not a char array). here's the code Code #include <stdio.h>RE: const char *" cannot be assigned to an entity of type - Annabelle Lee - May 6, 2018 (May 6, 2018 05:52 PM)maryous Wrote: here's the code This code compiles and runs for me. My text file for output contains the following: Code };RE: const char *" cannot be assigned to an entity of type - maryous - May 6, 2018 (May 6, 2018 05:57 PM)Annabelle Lee Wrote: This code compiles and runs for me. My text file for output contains the following: could it be a different compiler ? im using visual studio 2017 RE: const char *" cannot be assigned to an entity of type - Annabelle Lee - May 6, 2018 (May 6, 2018 05:59 PM)maryous Wrote: could it be a different compiler ? It compiled fine for me on VS2017 and gcc. RE: const char *" cannot be assigned to an entity of type - maryous - May 6, 2018 (May 6, 2018 06:10 PM)Annabelle Lee Wrote: It compiled fine for me on VS2017 and gcc. that's really weird ... I have no idea why i have this issue I'm using windows 10 latest update (doubt windows can effect this) RE: const char *" cannot be assigned to an entity of type - 0xDEAD10CC - May 7, 2018 Are you sure you're not compiling as C++? That's the issue that nobody has really caught here. You may be trying to write C code, but you're definitely not invoking the C compiler... Btw, that code should be valid C, but it's a very stupid thing to do. Assigning a literal like "testing" to a char pointer means that you can write code to modify that string, but it won't warn you about the fact that it can't actually do it. You're getting a non-const pointer to read only memory. RE: const char *" cannot be assigned to an entity of type - Hoodie+ - May 7, 2018 Code #include <stdio.h>Compile, run and enjoy. 🤝 RE: const char *" cannot be assigned to an entity of type - maryous - May 7, 2018 (May 7, 2018 09:39 AM)0xDEAD10CC Wrote: Are you sure you're not compiling as C++? That's the issue that nobody has really caught here. You may be trying to write C code, but you're definitely not invoking the C compiler... Maybe that's the issue. I will check now and see if it can compile and i didn't know there's a difference between the compilers since i use VS2017 Also im still learning and i was reading codes and applying the concept to build a very simple runPE RE: const char *" cannot be assigned to an entity of type - Annabelle Lee - May 7, 2018 (May 7, 2018 12:34 PM)maryous Wrote: Maybe that's the issue. I will check now and see if it can compile and i didn't know there's a difference between the compilers since i use VS2017 If you are using VS2017, make sure your file ends with .c, and not .cpp RE: const char *" cannot be assigned to an entity of type - Gorzoid - May 7, 2018 OP simply changing compiler is not the solution. you need to be aware of const correctness, your lucky the compiler caught you out on this, otherwise you'd have alot of problems in the future when you try to write to readonly memory. |