Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

const char *" cannot be assigned to an entity of type

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) {
arr_name = argv[2];
}
else {
arr_name = "filedata";
printf("unsigned char %s[] = {", arr_name);
}
REP ME, ILL REP YOU

This: arr_name = "filedata"; should be: arr_name = 'filedata'; I believe that'll solve your issue.
(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>

int main(int ac, char **av) {
        char *arr_name;

        if (ac >= 3) {
                arr_name = av[2];
        } else {
                arr_name = "filedata";
                printf("unsigned char %s[] = {", arr_name);
        }

}

Which is a totally legal C code (and compiles perfectly fine). Please post your whole code.
(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"
REP ME, ILL REP YOU

(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).




@OP: From what I undertstands your code looks like this:
Code
#include <stdio.h>

int main(int ac, char **av) {
       char *arr_name;

       if (ac >= 3) {
               arr_name = av[2];
       } else {
               arr_name = "filedata";
               printf("unsigned char %s[] = {", arr_name);
       }

}

Which is a totally legal C code (and compiles perfectly fine). Please post your whole code.

Oh I see where I went wrong, not sure why I assumed it the way I did. Thanks for catching that.
(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).




@OP: From what I undertstands your code looks like this:
Code
#include <stdio.h>

int main(int ac, char **av) {
       char *arr_name;

       if (ac >= 3) {
               arr_name = av[2];
       } else {
               arr_name = "filedata";
               printf("unsigned char %s[] = {", arr_name);
       }

}

Which is a totally legal C code (and compiles perfectly fine). Please post your whole code.




here's the code

Code
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

FILE *f;
int i, c;
char *arr_name;

if (argc < 2) {
fprintf(stderr, "Usage: %s input_file [array_name] [> output_file]\n", argv[0]);
return 1;
}
f = fopen(argv[1], "rb");
if (f == NULL) {
fprintf(stderr, "%s: fopen(%s) failed", argv[0], argv[1]);
return 1;
}

if (argc >= 3) {
arr_name = argv[2];
}
else {
arr_name = "filedata";
printf("unsigned char %s[] = {", arr_name);
}
for (i = 0;; i++) {
if ((c = fgetc(f)) == EOF) break;
if (i != 0) printf(",");
if ((i % 12) == 0) printf("\n\t");
printf("0x%.2X", (unsigned char)c);
}

printf("\n\t};\n");
printf("unsigned int size = %i;", i);
fclose(f);
return 0;
}
REP ME, ILL REP YOU

(May 6, 2018 05:52 PM)maryous Wrote:  here's the code

Code
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

FILE *f;
int i, c;
char *arr_name;

if (argc < 2) {
fprintf(stderr, "Usage: %s input_file [array_name] [> output_file]\n", argv[0]);
return 1;
}
f = fopen(argv[1], "rb");
if (f == NULL) {
fprintf(stderr, "%s: fopen(%s) failed", argv[0], argv[1]);
return 1;
}

if (argc >= 3) {
arr_name = argv[2];
}
else {
arr_name = "filedata";
printf("unsigned char %s[] = {", arr_name);
}
for (i = 0;; i++) {
if ((c = fgetc(f)) == EOF) break;
if (i != 0) printf(",");
if ((i % 12) == 0) printf("\n\t");
printf("0x%.2X", (unsigned char)c);
}

printf("\n\t};\n");
printf("unsigned int size = %i;", i);
fclose(f);
return 0;
}

This code compiles and runs for me. My text file for output contains the following:

Code
    };
unsigned int size = 0;
(May 6, 2018 05:57 PM)Annabelle Lee Wrote:  This code compiles and runs for me. My text file for output contains the following:

Code
};
unsigned int size = 0;

could it be a different compiler ?
im using visual studio 2017
REP ME, ILL REP YOU

(May 6, 2018 05:59 PM)maryous Wrote:  could it be a different compiler ?
im using visual studio 2017

It compiled fine for me on VS2017 and gcc.
(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)
REP ME, ILL REP YOU

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.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Code
#include <stdio.h>

int main(int argc, char**argv)
{
    const char *arr_name;

    if (argc >= 3) {
        arr_name = argv[2];
    }
    else {
        arr_name = "filedata";
        printf("unsigned char %s[] = {", arr_name);
    }

    return 0;
}

Compile, run and enjoy. 🤝
(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...

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.

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
REP ME, ILL REP YOU

(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
Also im still learning and i was reading codes and applying the concept to build a very simple runPE

If you are using VS2017, make sure your file ends with .c, and not .cpp
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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by maryousJul 24, 2022 03:35 PM 0 162 Jul 24, 2022 03:35 PM
Last Post: maryous
[.NET] by gijivargijiAug 10, 2021 09:06 PM 4 217 Aug 10, 2021 09:06 PM
Last Post: Shadow_Cloud
[.NET] by KαѕρerDec 3, 2020 06:42 AM 1 122 Dec 3, 2020 06:42 AM
Last Post: Rmdy