Need help in pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manjuks
    New Member
    • Dec 2007
    • 72

    Need help in pointer

    Hi All,

    int *p = 100;

    printf("%d\n", *p) //It will give segmentation fault
    printf("%d\n", p) ; //It will print 100;

    here *p behaving as normal variable. why so?

    Thanks,
    Manjunath
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Because p is a normal variable. Its type, int *, only defines the operations you can do with it, you can use additional and subtraction, you can't use multiplication or division, you can dereference, you can use it as a function parameter to a function prototyped to take another type.

    I would have thought that most modern compilers would give a warning (or error) for assigning an integer to a pointer.

    The segmentation fault is because 100 is not a valid address in memory but you tried to access it.

    Comment

    • jabbah
      New Member
      • Nov 2007
      • 63

      #3
      Maybe the problem lies in this line:

      int *p = 100;
      are you sure you didnt want to:

      int i = 100;
      int* p = &i;

      Comment

      • alexis4
        New Member
        • Dec 2009
        • 113

        #4
        manjuks,

        I always feel saver writing it like
        (*p) = 100;
        It represents the reality, something useful for complex operations. Either way, I think that all three statements you are using are not correct anyway. Did you compiled it without errors?
        Generally you should pass the data of your pointer to a variable first and then use this variable in printf(). If somebody wants to understand pointers he should search for their use in functions, where they permit multiple function outputs.

        Comment

        • sidooh
          New Member
          • Dec 2009
          • 9

          #5
          *p means the value stored at address 100.
          p means the address 100.

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            to Banfa
            I would have thought that most modern compilers would give a warning (or error) for assigning an integer to a pointer.
            Yes...I use Dev-C++ ver 4.9.9.2 which people say is out of date etc but it won`t allow the above.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              When I started programming, 20 years ago, we used that exact syntax to highlight areas of the code that need looking at. Because our code compiled without warnings the warning produced by that statement insured that the issue didn't get overlooked.

              In C++ it should produce an error as it is prohibited by the standard. You can't assign to a pointer like that in C++ without a reinterpret_cas t.

              C is more... flexible, but I think most compilers today would produce a warning but I guess they may not if the warning level is set low enough.

              Comment

              Working...