Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer and string

Status
Not open for further replies.

manichandra

Technical User
Feb 6, 2012
19
US
why can't we do this ??

int main()
{
char *p="MANI";;
*(p+1)='C';
*(p+2) = 'B';
*(p+3) = 'D';

printf("%s\n",p);
return 0;
}

when we can do this ..

int main()
{
char s[]="MANI";
char *p;
p=s;
*(p+1)='C';
*(p+2) = 'B';
*(p+3) = 'D';
printf("%s\n",s);
printf("%s\n",p);
return 0;
}

 
In the first case, you are trying to change the contents of a constant. It will fall over if you tried

*(p+5) = 'x';

because you haven't allocated the space for it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top