EmbLogic's Blog

problem

#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…

4 Responses to problem

  1. Ram D says:

    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);
    }
    }

  2. jasmeen.kaur says:

    Can u pls tell what is written before *p in printf statement

  3. jasmeen.kaur says:

    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’

  4. *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

Leave a Reply to jatinder.kumar Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>