+ 7
Some problems with string
I have just reached to strings in c language... "when you declare a char pointer like char *p="String"; it will be constant " I tried to ensure of this put I couldn't print *p Why? the compiler take the pointer as integer not as a string https://code.sololearn.com/cLn5IBVBgtik/?ref=app
10 ответов
+ 4
ABADA S I guess they meant to say that it can't be changed because the pointer *str points to a string literal rather than a string variable, a string literal can't be changed as their value is not stored in a variable, hence we can't modify its content. I'm not really good in explaining, especially if it comes to pointers ; )
I hope someone more knowledgeable can drop some extra hints on this, to get a better comprehension : )
+ 7
Prints "ABADA", notice you don't need dereference operator '*' before 'x'.
printf("%s", x);
Prints 'A', the first char in 'x'.
printf("%c",*x);
+ 6
A string pointer declaration such as char *str = "stuff"; is considered a constant and cannot be changed from its initial value.
that what is written exactly but the string is not constant Ipang
+ 6
thank you
+ 6
thank you I think I got it
both of strings have been existed befor compiling and the pointer is just indicating to it so if I write again x="ABADA"; the address will be the same to the first address
what does %p means
+ 5
so I can change the string to other one with regardless of length but I can't change a single char ... isn't it?
+ 5
thank you very much master Ina and thanks for who try to help me
I appreciate your aid
+ 4
It doesn't take the pointer as integer, but if you use format specifier %d it will implicitly cast char to int and show its ASCII value.
As you dereferenced, it will output the value at the position that x points to (which is a char A).
Using strings you do not need to derefernce it, as Ipang said. You only need the starting position x and because you tell via format specifier %s that it is a string, it will print all characters until the termination character '\0' is hit.
This termination character has been included automatically when defining the string.
+ 3
ABADA S I'm sorry, I haven't read the C lesson yet, so I don't really understand what you mean by "they said that it can not be changed", what exactly they said can't be changed? elaborate some more maybe?