Some problems with string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

28th Sep 2018, 5:39 AM
ABADA S
ABADA S - avatar
10 Answers
+ 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 : )
30th Sep 2018, 4:01 AM
Ipang
+ 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);
28th Sep 2018, 6:41 AM
Ipang
+ 6
thanks you Ipang & Matthias I got it but in the lesson they said that it can not be changed .. I edited the code below and changed the value so what was that mean
28th Sep 2018, 3:09 PM
ABADA S
ABADA S - avatar
+ 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
28th Sep 2018, 8:44 PM
ABADA S
ABADA S - avatar
+ 6
thank you
30th Sep 2018, 7:56 AM
ABADA S
ABADA S - avatar
+ 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
30th Sep 2018, 5:30 PM
ABADA S
ABADA S - avatar
+ 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?
30th Sep 2018, 4:46 PM
ABADA S
ABADA S - avatar
+ 5
thank you very much master Ina and thanks for who try to help me I appreciate your aid
30th Sep 2018, 6:07 PM
ABADA S
ABADA S - avatar
+ 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.
28th Sep 2018, 6:55 AM
Matthias
Matthias - avatar
+ 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?
28th Sep 2018, 5:22 PM
Ipang