I simply make a char array str.later i made a char *p
which simply pointing to it,i can access(travel ) from starting to end the str
, can i manipulate the str
using p
, means
p[i]='x';
(where i : a valid index ,x:any charater ) is valid?? actully i tried it it is working ,my question is is there no need to give it memory using malloc(i.e. p=malloc(l*sizeof(char));).
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
char *p;
scanf("%s",str);
printf("%s\n",str);
int l=(int)strlen(str);
// printf("l= %d\n",l);
//p=str;
//p=malloc(l*sizeof(char));
p=str;
int i;
for(i=0;i<l;++i)
printf("%c-",*(p+i));
printf("\nNow p=%s\n",p);
p[1]='x'; // it is valid , but difficult to understand?? we didnot give it memory,but it can manipulate the str as well.
printf("After changes made\n",p);
printf("p=%s\n",p);
printf("str=%s\n",str);
return 0;
}