Empty value detection for char* type variable

sorry for misunderstanding

                    s addr 0xffffcbd0 - 11 68 65 6c 6c 6f 20 77 6f 72 6c 64
                p = 0 addr 0x0
             p george addr 0x100403024 -  6 67 65 6f 72 67 65
Segmentation fault (core dumped)
    p = (char*) "george";
    dispStr (p, "p george");

    *p = 'G';
    dispStr (p, "p G");

still confused by your comments. why worry about where it is allocated? and why the discussion of nullptr?

while a compiler can store a const char in non-volatile memory, i don't believe that's the primary purpose of const char. as you've pointed out, I believe it is to generate a compile or run time error when there is an attempt to modify such a string.

  1. i think there needs to be a clear understanding that anything defined within quotes, "" is a constant. an attempt to modify such a string (e.g. *p = 'G') results in an error, even in C

  2. passing a const char to a sub-function that modifies a string defined as char * results in a run-time error while defining the argument as const char * results in a compile time error. this was my view on the value of const char

  3. another behavior is defining const char ptr, e.g. const char *t = "hello"; prevents changing the value of the ptr

all 3 of the above behaviors are sensible (if you know what you're doing) and can prevent problems if just blindly used (if you're unsure)

in both cases: "" and "ab", the strings are null terminated and allocated in a section of memory that is not intended to be written. this space would be different that where other variables are allocated.

you can see that "p" has significantly different address values when set to "george" at 0x100403024 vs the char array s at 0xffffcbd0

s addr 0xffffcbd0 - 11 68 65 6c 6c 6f 20 77 6f 72 6c 64
                p = 0 addr 0x0
             p george addr 0x100403024 -  6 67 65 6f 72 67 65
                p = s addr 0xffffcbd0 - 11 68 65 6c 6c 6f 20 77 6f 72 6c 64
             s zeroed addr 0xffffcbd0 -  0

Could be (significantly) different. Depends on the specific architecture.

why?

the compiler separates code ("text") from data ("data" and "bss"). one reason is to allow memory protection hardware to prevent code from overwriting code as well as outside the memory allocated for the task. and of course this allows the text segment to more easily be allocated to non-volatile memory (flash) on things like AVR processors.

back when ROM and RAM were used, the text segment has to be located within the address space of the ROM and the data in the RAM space which has to be explicitly specified to the linker for the particular hardware being used.

Please show the complete code that produces the output you posted.

see post #12

That’s a possible compiler decision depending on the underlying environment.

My point is just that you should not use char * for a pointer if you want to initialize it to pointing to something that is defined within double quotes (as this is const).

Exactly my point and what you should not do and that will raise a warning

(Yes it allocated 1 byte which is null but it’s constant)

1 Like

the compiler reminds you of this with a warning (see post #16, "ISO C++ forbids).

but you may want to use a char * ptr if it needs to be changed. (i think this is comparable to "const int MyNum = 56;" which relies on the compiler to do what the preprocessor, "#define MyNum 56")

I think We agree on that, and that’s why I’m insisting that program should not do what was written in the first post

char* token ="";

Or what you wrote

p = (char*) "george";

This cast is not legit in C++ (and it still is I think in C)

C doesn't have "const char"

 p george
Segmentation fault (core dumped)
#include <stdio.h>

int
main ()
{
    char *p = "george";
    printf (" p %s\n", p);

    *p = 'G';
    printf (" p %s\n", p);
}

why?

what if i want a string point to have a default string value (e.g. "no value set") but is later set to a dynamically created string (e.g "value is 29").

Then use a char array.

Because the language says so. That was the point I was trying to make, if you want a non const data pointer, it needs to point to non const data so you define

char p[] = "George";

P still is a pointer to char but the char data is not constant

That’s was also the whole purpose of

char nullChar = '\0';
char * pempty = & nullChar;

nullChar is modifiable so I can use its address to initialize the pempty pointer

of course.

main: default
main: today is weds
#include <stdio.h>

int
main ()
{
    char  s[80];
    char *p = (char*) "default";

    printf ("%s: %s\n", __func__, p);

    sprintf (s, "today is %s", "weds");
    p = s;
    printf ("%s: %s\n", __func__, p);
}

please explain it to me

I know, but it was just a way so make things easier to understand, not a programming suggestion.
:wink:

This is not good in C++… the text is a const char *

I give up….

I can’t explain it better than the compiler when it tells you

warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
24

It’s forbidden … just accept the fact (because A string literal’s type is const in C++)

it's a "warning" not an error

I don’t understand this either - but let’s keep it aside…. We have polluted this thread long enougv

Well the word forbid has a meaning, right… when something is forbidden you should not do it if you want to respect the law of the land.

Some compiler or architectures will refuse to compile