MCS-201 · Unit 7

Pointers

Section 2 of 7

Block 2 · Unit 7Pointers12 min · Section 2 of 7

7.2Declaring and Assigning Pointers

Covers IGNOU MCS-201 §7.4, 7.4.1, 7.4.2

Like every variable, a pointer must be declared before use, with a type that says what kind of object it points to. This section covers declaring pointers, giving them valid addresses, and three special kinds: pointers to pointers, null pointers and void pointers.

Declaring a pointer

Syntax
data-type *pointer-name;

int *ptr; declares ptr as a pointer to int. The type before the * is the type of the object pointed to, not the type of the pointer’s own value. That type tells the compiler how many bytes to read through the pointer and how to interpret them.

In a declaration, the * belongs to each name: int *p, q; declares p as a pointer and q as a plain int.

Assigning an address and using the pointer

Memory after: int qty = 5; int *ptr = &qty;
   variable:   ptr              qty
   value:    [ address of qty ] [ 5 ]
                    |             ^
                    +-------------+

ptr = &qty; stores the address of qty in ptr. After that, *ptr is another name for qty: reading *ptr reads qty, and assigning to *ptr changes qty.

Uninitialised pointers

A pointer declared without a value holds garbage: it points to some unknown place. Writing through it changes memory that does not belong to the variable you intended.

Pointer to a pointer

A pointer is itself a variable with an address, so another pointer can point to it. int **pii; declares a pointer to a pointer to int.

int i = 100; int *pi = &i; int **pii = π
   pii  ----->  pi  ----->  i
                             100
   **pii   is   *pi   is   i

Each * follows one arrow: *pii is pi, and **pii is i.

Null pointers

The null pointer is the standard “points to nothing” value. Set a pointer to NULL when it has no valid target, and test it before use: if (ptr != NULL) { ... }. Dereferencing a null pointer is undefined behaviour and normally crashes the program at once, which is far easier to debug than a garbage pointer.

void pointers

A void * is a generic pointer: it can hold the address of an object of any type, but it carries no information about that type. It cannot be dereferenced directly; it must first be converted to a pointer of a real type.

Key takeaways

  • int *p; declares a pointer to int; p = &x; makes it point to x.
  • *p reads or writes the object pointed to.
  • An uninitialised pointer is dangerous; initialise to a valid address or NULL.
  • int **pp points to a pointer; void * is a generic pointer that needs a cast before use.

Practice questions

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

Question 1 · multiple choice

After int a = 3, *p = &a; *p = 9;, what is the value of a?

Show answer

Answer: B. 9

*p is another name for a, so assigning 9 to *p changes a to 9.

Question 2 · multiple choice

In int *p, q;, what are the types of p and q?

Show answer

Answer: C. p is a pointer to int and q is an int

The * applies only to the name it is attached to. To declare two pointers, write int *p, *q;.

Question 3 · multiple choice

What is the purpose of NULL?

Show answer

Answer: D. It indicates that a pointer points to no object

A null pointer is a recognisable “points to nothing” value that can be tested before the pointer is used.

Question 4 · multiple choice

What does this program print?

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

int main(void)
{
    int v = 4;
    int *p = &v;
    int **pp = &p;
    **pp = **pp * 3;
    printf("%d\n", v);
    return 0;
}
Show answer

Answer: C. 12

**pp is v, so the statement means v = v * 3, giving 12.

Question 5 · multiple choice

Which statement about a void * pointer is correct?

Show answer

Answer: A. It can hold the address of any type but must be cast before it is dereferenced

A void * is generic, so the compiler does not know the size or type of the target until the pointer is converted to a real pointer type.

Question 6 · short answer

Explain what is wrong with int *p; *p = 25; and write a corrected version.

Show worked answer

p is declared but never given an address, so it holds a garbage value and points to an unknown location. *p = 25; writes 25 to that location, which is undefined behaviour: it may crash or silently corrupt other data. Corrected version: int x; int *p = &x; *p = 25;. Now p holds the address of a real variable, so the assignment stores 25 in x.

↑ ↓ move↵ openTab switch type