pointers trivia

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Encrypted
    New Member
    • Mar 2008
    • 8

    pointers trivia

    i have two questions right now in my mind :

    question - 1 :

    please have a look at this code :

    Code:
    void function (int recd)
    {
       printf ("%d", recd);
    }
    void main()
    {
      int p=10;
      function(p);
    }
    here...when p is passed to function, the thing that actually takes place is :
    a new variable recd is created and value of p is copied to recd (i mean a copy of p is created...consu ming another 2 bytes)...

    now please check the following code :

    Code:
    void function (int *recd)
    {
       printf ("%d", *recd);
    }
    void main()
    {
      int *p;
      *p=10;
      function(p);
    }
    does the same thing happen here?? i mean here too.. new pointer variable recd is created and value (an address) stored in pointer p is copied to recd???


    Code:
    void function (int *recd)
    {
      printf ("%d", *recd);
    }
    
    void main()
    {
       int p=10;
       function(&p);
    }
    and here too, does the same thing happens???
    please throw some light on it... :)


    question - 2:
    Code:
    void main()
    {
       int **p;
       **p = 10;   
    }
    in the above code, where the hell is 10 stored???
    what exactly happen in above code??
    my guess is : its stored in *p...
    i mean **p has a garbage which is taken as *p...and *p has a garbage which is taken as the address of 10....is this right or a garbage?? :P :)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Answer to Question 1: All variables passed to functions are copied. This is called pass by value and it is all there is in C. The same is true of C++ except that there is a reference function argument that is not a copy of the variable used on the call but becomes a second name for that variable. The reference was developed to avoid the error-prone C pointer syntax.

    The fact that the variable used on the call is a pointer is irrelevant. The function has a copy of the pointer. It's just that by using the contents of the pointer a variable outside the function can be accessed but that is a feature of the pointer's data and not the pointer itself. In A and C++ a pointer is a variable like any other.

    Answer to Question 2: Indeterminate behavior. It depends on your compiler. Visual Studio.NET generates a warning that an uninitialized variable is being used, but it's just a warning. Ultimately, you are responsible for memory.

    Comment

    • Encrypted
      New Member
      • Mar 2008
      • 8

      #3
      heyyy...thnxx..
      i got your points ... :)

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        In your second section of code, I believe you will suffer the same errors as the final code portion: that is, you have:

        Code:
        int *p;
        *p = 10;
        p has no value (since you have not used new (C++) or malloc (C)), and so you are putting 10 into some random location in memory.

        Comment

        Working...