there's a difference between the pointer pointing to nothing, it's value is zero and it pointing to a NULL string (i.e. "") where the first char in the string it is pointing to is NULL (i.e '\0')
that's bad. You have a pointer to non constant char data and you assign a pointer to constant char data... The compiler will bark (at least on an ESP, the AVR options are more lenient)
for your test you want to do
If( *token != '\0' ){
to test the content of the byte you point to.
char emptyChar = '\0';
char* token = & emptyChar;
If( *token != emptyChar ){
Serial.println(F("the cString you point at is not empty"));
}else{
Serial.println(F("the cString you point at is empty"));
}
note that this initialization form is allowed to constant strings only. So your token never changed and will be always contains the empty string and your if condition is completely non-sense
Just to recap and simplify a bit the suggestions others gave you, you first need to specify what "empty" means for you.
A "char*" is a pointer, thus "empty" pointers are NULL (a "zeroed" address), so you test it by comparing it to NULL: if (token == NULL)
An "empty string" is a char* pointer (so it's not NULL) pointing to a zero (0x0 or '\0') byte, and you test it by comparing the first pointed byte: if (*token == 0)
your summary is not really great, you missed the point that "" is a constant cString, so you can't assign its pointer to a non constant cString. Also 0 should be discouraged in C++ for the null pointer in favor of nullptr.
I canât test right now but What happens if you do
*p = âGâ;
After assigning george to p
Also why did you take care of defining the second parameter of your function as const char *label â why const if you donât need it for string literals?
not sure what your asking for?
the dispStr() expects a null terminated string.
also assume you mean p = &'G'. do you just want to see the value (addr) of p
i got a warning
main.cpp:24:17: warning: ISO C++ forbids converting a string constant to âchar*â [-Wwrite-strings]
24 | dispStr (s, "s");
but why do you say "if you don't need it for string literals"?
i'm not sure i fully understand the motivation for the C++ thing about "const char". (i just noticed that it's just a warning).
main.cpp:29:10: warning: ISO C++ forbids converting a string constant to âchar*â [-Wwrite-strings]
29 | p = "george";
but presumably it tells the compiler that the string it not to be modified. unlike other parameters passed to a function by value, while the string ptr may be passed by value, it exposes the string to manipulation in the function. by defining it as "const char", you're asking the compiler to generate an error(?) if there's code that does modify it.
i get following if i define q as const char in distStr() and attempt to change the string
main.cpp: In function âvoid dispStr(const char*, const char*)â:
main.cpp:17:8: error: assignment of read-only location â* qâ
17 | *q = 0;
actually both parameters should be defined as const char
so a string passed to a function that is simply a label should be defined as const char since most of the time that is what is passed
I was asking you to try to change the lower case g from âgeorgeâ into G and print it again.
It is possible that nothing would happen
String literals are constant, depending on the architecture smart compiler would store those strings into non modifiable memory segments, could be flash memory.
Also as they are constant, the compiler is free to optimize the memory and reuse substrings if it identifies some. Try this
On AVR the compiler does store the data in SRAM but the optimizer notices that it can reuse some of the memory for the various values. As they are supposed to be const thatâs fine, but if you use the pointer to modify the SRAM for the content pointed by p1, youâll see that you actually modified the three pointed values. The memory was shared.
Thatâs why you need const and then the compiler wonât let you do dangerous stuff
No itâs just that the C++ rule states that "george" is a constant string literal and the compiler has this expectation. If you do funny things then with the data because you did not use the const qualifier for the pointer then you are in uncharted territory.
So long story short respect the types and the modern standards
If your pointer needs to point at modifiable data then use such a pointer hence my
char nullChar = '\0';
char * pempty = & nullChar;
Or use the null pointer which works for everything
char * pnull = nullptr;
if you use const data then declare the pointer accordingly
const char * pEmpty = "";
or use
char pEmpty[] = "";
Which leads to memory allocation in SRAM
If you donât respect the const qualification then the compiler will bark at you with warning: ISO C++ forbids converting a string constant to âchar*â
or possibly even stop depending on stricter compilerâs option.
Note: it used to be fine if I remember until C++11 to cast and C has allowed it as well due to so much code depending on this.
Arduino is using a C++ compiler so what we know about C does not always apply. C++ is stricter on types.