Pointers -C: Doubt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShyamCbe
    New Member
    • Jul 2007
    • 1

    Pointers -C: Doubt

    Hi Buddies

    Can u please help me in understanding these codes and their differences.

    Code 1

    void main()
    {
    int *p=NULL;
    scanf("%d",&p);
    printf("\np value: %d",p);
    }


    Code 2

    void main()
    {
    int *p=NULL;
    scanf("%d",p);
    printf("\np value: %d",*p);
    }

    What will be the result of these code and their differences in storage?


    Thanks and Regards
    Shyam
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by ShyamCbe
    Hi Buddies

    Can u please help me in understanding these codes and their differences.

    Code 1

    void main()
    {
    int *p=NULL;
    scanf("%d",&p);
    printf("\np value: %d",p);
    }


    Code 2

    void main()
    {
    int *p=NULL;
    scanf("%d",p);
    printf("\np value: %d",*p);
    }

    What will be the result of these code and their differences in storage?


    Thanks and Regards
    Shyam
    Hi Shyam,

    First code is correct as in scanf & is preferred and it prints the value given as an output. Second code gives segmentation fault.

    Regards

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Hi,
      Both the code results in segmentation fault.
      Since the P is null, u will get this.
      Bay the way, have u tried executing this.?
      Raghuram

      Comment

      • Meetee
        Recognized Expert Contributor
        • Dec 2006
        • 928

        #4
        Originally posted by gpraghuram
        Hi,
        Both the code results in segmentation fault.
        Since the P is null, u will get this.
        Bay the way, have u tried executing this.?
        Raghuram
        Hi,

        I am running the same code-1 on linux and when I enter value 5 it gives value: 5!!!!!!
        It doesn't give segmentation fault!!

        Regards

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by zodilla58
          Hi,

          I am running the same code-1 on linux and when I enter value 5 it gives value: 5!!!!!!
          It doesn't give segmentation fault!!

          Regards
          That might be undefined behaivior. I'm not sure but I think the value the user enters in code 1 is set to the address that the pointer points to, so when you print the pointer you get the same value. I'm not sure about it though.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by zodilla58
            Hi,

            I am running the same code-1 on linux and when I enter value 5 it gives value: 5!!!!!!
            It doesn't give segmentation fault!!

            Regards
            I suspect that on your C/C++ impementation sizeof(int) == sizeof(int *).

            Code 1 erroneously stores the integer value input in the location of a variable of type int * and then treats that variable as it it were an int when it prints it out.

            It doesn't give a segmentation fault but is really quite wrong and the code is likely to stop working properly on any system where sizeof(int) != sizeof(int *).

            Comment

            Working...