2080 2

CSC115 ยท 2080 ยท 10 marks

Explain the relation to array and pointer.Differentiate between call by value and call by reference with a suitable program.[2+8]

Explain the relation to array and pointer.Differentiate between call by value and call by reference with a suitable program.[2+8]

C ProgrammingChapter 9: PointersPointers and Arrays

Quick answer

In C, there is a strong relationship between arrays and pointers. Any operation that can be achieved by array subscripting can also be done with pointers.

Relation Between Array and Pointer, and Call by Value vs Call by Reference


(a) Relation Between Array and Pointer

Key Points:

  • An array name is itself an address (a constant pointer to the first element of the array).
  • Pointers and arrays are almost synonymous in terms of how they are used to access memory.
  • The pointer version of array operations is generally faster but harder to understand for beginners.

Illustration:

int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;   // ptr points to the first element of arr
Array NotationPointer NotationMeaning
arr[0]*ptrFirst element
arr[1]*(ptr + 1)Second element
arr[i]*(ptr + i)i-th element
&arr[i]ptr + iAddress of i-th element

Important Difference:

  • A pointer is a variable (its value can be changed).
  • An array name is a constant pointer (it always points to the first element and cannot be reassigned).

(b) Difference Between Call by Value and Call by Reference

Definition:

Call by Value: In call by value, a copy of the actual argument is passed to the formal parameter. The called function works on this copy, so any changes made inside the function do not affect the original variable.

Call by Reference: In call by reference, the address (reference) of the actual argument is passed to the function. The called function works directly on the original data using pointers, so changes made inside the function do affect the original variable.


Comparison Table:

FeatureCall by ValueCall by Reference
What is passedCopy of the valueAddress of the variable
Effect on original dataNo change in original variableOriginal variable gets modified
MemorySeparate memory for formal parameterFormal parameter refers to same memory
Return valuesCan return only one valueCan effectively return multiple values
SafetySafer (original data protected)Less safe (original data can be changed)
SpeedSlightly slower (copy is made)Slightly faster (no copy made)
Mechanism usedNormal variablesPointers

Program Demonstrating Both:

#include<stdio.h>
#include<conio.h>

/* Function prototypes */
void callByValue(int x);
void callByReference(int *x);

void main()
{
    int a = 15;
    int b = 15;

    clrscr();

    /* Demonstrating Call by Value */
    printf("--- Call by Value ---\n");
    printf("Before calling function, a = %d\n", a);
    callByValue(a);
    printf("After calling function, a = %d\n\n", a);

    /* Demonstrating Call by Reference */
    printf("--- Call by Reference ---\n");
    printf("Before calling function, b = %d\n", b);
    callByReference(&b);
    printf("After calling function, b = %d\n", b);

    getch();
}

/* Call by Value: works on a copy */
void callByValue(int x)
{
    x = x + 5;
    printf("Inside callByValue function, x = %d\n", x);
}

/* Call by Reference: works on original via pointer */
void callByReference(int *x)
{
    *x = *x + 5;
    printf("Inside callByReference function, *x = %d\n", *x);
}

Output:

--- Call by Value ---
Before calling function, a = 15
Inside callByValue function, x = 20
After calling function, a = 15

--- Call by Reference ---
Before calling function, b = 15
Inside callByReference function, *x = 20
After calling function, b = 20

Explanation of Output:

  • In Call by Value: a remains 15 after the function call because only a copy (x) was modified inside the function. The original variable a is not affected.

  • In Call by Reference: b becomes 20 after the function call because the address of b was passed (&b). Inside the function, *x directly modifies the original variable b. The original variable b is affected.


Summary: Use call by value when you want to protect the original data. Use call by reference when you want the function to modify the original variable or when you need to return multiple values from a function.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.