pointes problem

closed account (28poGNh0)
/// I knew that it is a bad thing to declare a pointer that points to nowhere so Is this fine ?

char *str = ""; //is this correcte

thanks
Yes it's fine , since your pointer points to a string-literal stored on memory.
That is fine, but if you have to declare a pointer and not initialize it yet you could do this:

1
2
char *str = NULL;  
// later in the code you can point *str to something else 
closed account (28poGNh0)
@BHXSpecter , I know that ,and thanks btw ,I still need to know if this "" means that I have a
string-literal stored on memory
closed account (28poGNh0)
Oh I think It is since it holds the \0 chatacter ,am I right?

closed account (o3hC5Di1)
Hi there,

Yes, when you initialize a C-string using a string literal, the null--character is automatically appended for you.
So strictly speaking the size of the char-array is actually 1, not zero, because it contains the null character. Of course, if you treat the char array as a string, it will tell you the string has 0 length because there is no content but the null character itself.

All the best,
NwN
"" is a string literal, just as "Hello, World!" and will be stored in the read-only data section of your program.

http://en.wikipedia.org/wiki/Data_segment#Data
http://en.cppreference.com/w/cpp/language/string_literal

So your code initializes a char pointer named str with the memory address of the string literal "".

And because the string literal is in the read-only section, the correct definition is:

const char *str = "";

Edit: to add, there are more kinds of literals than string literals.
http://en.cppreference.com/w/cpp/language/expressions#Literals
Last edited on
closed account (28poGNh0)
Thanks alot

@a k n , @BHXSpecter,@NwN, @Catfish4 and @Techno01 ,oh I am sorry thats me
Topic archived. No new replies allowed.