I just wanted to clarify about Character pointers and Arrays. Sorry for asking so much in one post but they are very short questions.. Thank you!
Code:
int main()
{
char *p="FIVE";
char k[]="FIVE";
cout << p << endl;
cout << k << endl;
}
1. Why is *p='s' invalid while k[0]='s' valid?
2. The double quote on the word FIVE means that the character pointer and the array ends with a NULL character. Right?
3. cout in this case just works like printf("%s", p) and printf("%s",k) right?
Code:
int main()
{
char *colour;
colour=new char[1];
colour="Black";
cout << colour <<endl;
}
4. "Use dynamic memory allocation for colour."-This was the instruction given in my assignment. But I don't really know what it means. Does the above code satisfy the instruction given?
5.
Code:
int main()
{
char *p="FIVE";
char k[]="FIVE";
char *s;
s=p;
strcpy(s,"WOW");
cout << s << endl;
cout << p << endl;
}
The above code compiles fine but shows an error when running the program. What I know: p contains the address of the character 'F'. This address is assigned to s.