MCS-201 · Unit 7

Pointers

Section 4 of 7

Block 2 · Unit 7Pointers14 min · Section 4 of 7

7.4Passing Pointers to Functions

Covers IGNOU MCS-201 §7.6, 7.6.1, 7.6.2

In Unit 5 a swap function failed because C passes arguments by value. Pointers solve the problem: pass the addresses of the variables, and the function can reach the originals. This section shows the technique and two related topics: returning several results, and functions that return pointers.

Call by reference using pointers

Call by valueCall by reference (pointers)
What is passedA copy of the valueA copy of the address
CallswapVal(x, y);swapRef(&x, &y);
Parametersint a, int bint *pa, int *pb
Access in the functiona, b*pa, *pb
Can change the caller’s variablesNoYes

Returning more than one value

A return statement gives back one value. To deliver several results, the caller passes the addresses of variables in which the function stores them.

Functions returning a pointer

A function can return an address; its return type is then a pointer type, such as int *find_max(int a[], int n). The address returned must still be valid after the function ends:

Safe to return the address ofNot safe
An element of an array passed in by the callerA local (automatic) variable of the function
A static or global variable
Memory obtained with malloc

A pointer to an object that no longer exists is called a dangling pointer.

Pointers to structures

When a pointer points to a structure, its members are reached with the arrow operator ->: p->member means (*p).member. Passing a structure’s address lets a function change the original without copying the whole structure.

C · struct_pointer.c
#include <stdio.h>

struct acc {
    int balance;
};

void deposit(struct acc *a, int amount)
{
    a->balance = a->balance + amount;     /* same as (*a).balance */
}

int main(void)
{
    struct acc mine = { 1000 };

    deposit(&mine, 500);
    printf("%d\n", mine.balance);
    return 0;
}
Output
1500

Key takeaways

  • Pass addresses (&x) and receive them in pointer parameters to change the caller’s variables.
  • The pointer itself is still passed by value.
  • Several results can be returned through pointer parameters.
  • A returned pointer must refer to something that still exists; p->m accesses a member through a structure pointer.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which call correctly uses void swapRef(int *a, int *b) to exchange m and n?

Show answer

Answer: C. swapRef(&m, &n);

The function expects addresses, which &m and &n supply. Passing m and n would pass values where pointers are required.

Question 2 · multiple choice

What does this program print?

C · triple.c
#include <stdio.h>

void triple(int *p)
{
    *p = *p * 3;
}

int main(void)
{
    int n = 7;
    triple(&n);
    printf("%d\n", n);
    return 0;
}
Show answer

Answer: B. 21

The function receives the address of n and stores 7 * 3 there, so n becomes 21.

Question 3 · multiple choice

Why does scanf("%d", &n); need the &?

Show answer

Answer: D. So that scanf receives the address of n and can store the input there

scanf must change the caller’s variable, so it needs its address; a copy of the value would be useless.

Question 4 · multiple choice

Which of these is unsafe for a function to return?

Show answer

Answer: A. The address of one of its own local variables

A local variable ceases to exist when the function returns, leaving a dangling pointer. The other three refer to memory that is still valid.

Question 5 · multiple choice

For struct pt { int x; int y; } s = {1, 2}, *p = &s;, which expression gives the member y?

Show answer

Answer: C. p->y

A structure pointer uses the arrow operator; p->y is the same as (*p).y. *p.y is wrong because . binds tighter than *.

Question 6 · short answer

Write a function min_max that receives an integer array and its size and gives back both the smallest and the largest element through pointer parameters, and show a call.

Show worked answer

void min_max(int a[], int n, int *min, int *max) { int i; *min = a[0]; *max = a[0]; for (i = 1; i < n; i++) { if (a[i] < *min) *min = a[i]; if (a[i] > *max) *max = a[i]; } }. Call: int lo, hi; min_max(marks, 5, &lo, &hi);. The addresses of lo and hi are passed, so the assignments to *min and *max inside the function store the two results directly in the caller’s variables.

↑ ↓ move↵ openTab switch type