Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Both pointer and reference variables take memory addresses as values. They provi

ID: 3822623 • Letter: B

Question

Both pointer and reference variables take memory addresses as values. They provide the power of indirect addressing, and are necessary for dynamic data structures. The main difference is in how de-referencing is performed. (a) Use an example to show how to define a pointer variable in your favorite language. Give code fragments in the same language to illustrate and explain the different ways pointer variables can be used in an assignment statement, both on the left and right hand sides. (b) Use an example to show how to define a reference variable in your favorite language. Give code fragments in the same language to illustrate and explain the different ways reference variables can be used in an assignment statement, both on the left and right hand sides. (c) Object variables in Java are a hybrid of pointer and reference variables. Use an example to show how to define an object variable in Java. Give code fragments to illustrate and explain when Java object variables behave like pointer variables and when they behave like reference variables.

Explanation / Answer

Please upvote :)

A pointer variable is just a variable that can store memory address. You can declare a pointer in the following ways

1. It's pretty simple to declare a pointer. All we have to do is put a * operator between the 'data type' and 'variable name'.
   int * ptr_int;
   char * ptr_char;
   int * ptr1, ptr2;

   a. Now, suppose that we have a integer variable 'x' which occupies some memory location. We then declare an pointer variable and initialize it to point to 'x'. Here '&x' gives the address of x in the memory and then we are assigning it to the pointer variable 'ptr'.
       int x = 10;
       int *ptr = &x;

   b. We can also assign an array to the pointer as follows. When assigning 'arr' to a pointer, we don't have to use '&arr' to get the address, as the 'arr' itself gives the base address of the array.

       int arr[] = {1, 2, 3, 4, 5};
       int * arr_ptr = arr;   // Here, arr is equal to &arr[0]
       int * arr_ptr = &arr[2]; // Here the pointer is pointing to the third element of the array
  
   c. you can even assign a pointer to null or make it point to a new memory location allocated by malloc. We can also assign one pointer to other.
       int *p1 = NULL;
       p1 = (int *) malloc(sizeof(int))
       int *p2 = p1;

   d. dereferincing a pointer (*). To get the value stored at the address pointed by the pointer, we use * operator.
  
       int x = 10;      
       int *p = NULL;
       p = &x;       // p now points to x
       int z = *p;    // z will now contains the value 10
       *p = 75;       // the address pointed by the pointer (and variable x), will now have a value of 75
       printf("x = %d", x);   // x will now hold the value 75  
              
   e. dereferencing with arrays
       int arr = {11,52,13,42,15,76,7};
       int *p = arr;
       int z = *(p+3)    // z will now hold the value of 43. It is equivalent to arr[3]
  


2. Reference variable cannot be re-assigned, and they must be assigned to a non-null value during initialization. We can think of it as an alias for the variable. Here, we are not creating an extra memory location, we are just creating a new alias/name for the same memory location.
   int x = 5;
   int &ref_var = x;

In the above code snippet, x and ref_var are both names for the same memory location. Consider the following code. Here n and a denotes the same memory location. When you declare a reference variable as a parameter, you are instructing the compiler to use the input variable memory space instead of allocating a new memory space during the function call.

   void func(int& n)
   {
        n = n + 1;
   }
   int a;
   func(a);

** All the assignments of the referece works like a normal variables.  

   int z = 10
   int &r = z;   // creating an alias for z
   r = 105;      
   printf("%d %d", z, r);    //z and r will give the same value of 100, as they are aliases for the same location.
   int k = z;   // k will be now 105
  
  
  
3. To define an object in java, we have to use the following syntax
   ObjectName var1;
   ObjectName var2 = NULL;
   ObjectName var3 = new ObjectName();

   In java everything is pass-by-value. To understand this, let's see an example
       Dog d = new Dog();
       func(d);

       ....
       ....

       public void func(Dog c) {
           c.setName("Ben");
           Dog someother = new Dog("Ken");
           c = someother;
       }

   Here d is not a Dog. We can think it as a pointer to the Dog. So when you pass it to a function, you are not actually passing the object, you are passing it's address. Now when you try to modify the "contents of the object" from inside the function, it will get reflected outside too. We set the name to "Ben", so it will get reflected everywhere.
   Note that c is just a reference variable. However, if you try to assign c to some other object, it will not work as it is just a reference variable. variable d will point to the same object outside the function. It won't point to the new Dog object. You can change the content of the object passed as a reference. But the changes to the reference variable itself has no effect to the actual object.

      
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote