char str[]="qwerty";
char *p="qwerty";
str++; \\error
*str='a'; \\works
p++; \\works
*p='k'; \\error

whereas,

char str[]="hello";
char *p="hello";
str="tell";\\ error
p="tell"; \\works

strings point to themselves like arrays
then why is

str="tell"; \\error

an error??
is the case same for arrays??
please explain the reason for every line.
also, why can't we assign a string to another but we can assign a char pointer to another???

char str[]="qwerty";
char *p="qwerty";
str++; \\error
*str='a'; \\works
p++; \\works
*p='k'; \\error

when you perform the operation str++..an error occurs ..this is because in c name of an array represents the starting address of an array...and the compiler remembers the starting address as that is the only information compiler has about an array and we cannot modify this address because in that case the compiler will have no idea as to where the array starts...
*str='a'
works because now the array which could be treated as it changes value at str[0]...
p++ works because it is a pointer that can point to any value..
*p='k' according to me this should also work
str="tell" gives an error because str returns address of str[0] which cannot hold an entire string

An array, as in an automatic/global variable like char ray[2];, cannot be used with ++ or any other trick that will change its value because it is an object-like context -- it doesn't hold a value per se but rather itself is that value, if that makes sense. This is, of course, somewhat philosophical rather than technical, but that's the idea. In technical terms, an array 'object' is not a valid lvalue, but that doesn't really help, does it? lol

*p = 'k' is an error because the pointer points to const memory -- all string literals are read-only(const). Assigning the pointer a heap address would be fine.

When you initialize a character array with a string literal, the compiler copies the string literal to the array. It doesn't actually 'point' to the string. The string literal can exist independently someplace else.

str = "tell"; is an incompatible types error. I find this odd since C hardly does(or doesn't at all?) any checks on types. I still think of it the same lines as the first paragraph, but in techincal terms I reckon the compiler doesn't know how to deal with this conundrum and that maybe the language itself doesn't really care. If they did decide to specify a behaviour for this, then it would create some ambiguities in the use of pointers(do you want to assign the address or the array 'object'?).

C is primitive in terms of string handling. It doesn't have the same ease of use and abstraction as something like Java and C#. But that's what makes it fun.

Note that C has no concept of object, or at least not in the same sense as that of other programming languages. I just put that in there to make it clearer(?).

I think the errors can be summarized thusly: an array is not a pointer and a string literal may not be modified.

But sir according to me string literals can be modified unless and until it has been declared as char const *p="hiii" or const char *p="hii"

But sir according to me string literals can be modified

According to you? How does your authority override the official language standard? I recognize that you might use a compiler that doesn't store string literals in read only memory, but that doesn't make attempts to modify them any less wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.