#include<stdio.h>
#include<string.h>
void main()
{
char *p=”abcdef”;
p = p + (strlen(p)-1);
printf(“%c\n”,–*p–);
printf(“%c\n”,–*–p);
printf(“%c\n”,–*(p–));
printf(“%c\n”,–*(–p));
printf(“%c\n”,*p);
}
unable to solve the mentioned problem pls help…
run following code in ur c99 compiler and compaire with the above one
#include
#include
void main()
{
char *p=”abcdef”;
for(int i=0;i<=6;i++)
{
p = p + (strlen(p)-i);
// printf("%c\n",–*p–);
// printf("%c\n",–*–p);
// printf("%c\n",–*(p–));
printf("%p\n",p);
printf("%c\n",*p);
}
}
Can u pls tell what is written before *p in printf statement
if it is — then –*p means u are changing the value at adress p(which is readonly) which will give you segmentation fault .
*p– means you are decrementing address i.e now it will point to ‘e’
*p get memory as read only. So when you printf(“%c\n”,–*p–); mesn you are goinh to change the contents of read only memory so that why you are getting segmantion fault