LearnFrenzy
Sign in Take a free test

C Programming · Topic 5 of 23

Pointers

  • 4 exercises
  • 40 questions
  • ~60 min
  • Answers explained

Introduction & Syntax

Pointers · Study material

A pointer in C is a variable that stores the memory address of another variable. Pointers let a function change the caller's variables, walk through arrays and strings, build linked lists and trees, and manage memory with malloc() and free(). They are also the topic interviewers and placement tests ask about most. This page explains pointers step by step, with programs whose output we have checked on a real compiler, and then gives you 40 practice questions: MCQs, predict-the-output, find-the-error and pointer programs.

On this page

  • Addresses, the & and * operators and pointer syntax
  • NULL pointers, pointer arithmetic and pointers with arrays
  • Call by reference, pointer to pointer, const and void pointers
  • Common pointer mistakes and how to avoid them

What is a pointer in C?

Memory is a long row of numbered bytes. When you write int marks = 85;, the compiler reserves 4 bytes at some address – say 2000 – and stores 85 there. The expression &marks gives that address. A pointer is simply a variable that holds such an address.

A variable and a pointer to it

  • marks85stored at address 2000
  • p2000int pointer – it "points to" marks
  • *p85the value at the address p holds

Pointer syntax: declaring, & and *

Declare a pointer by writing * between the type and the name. The type says what kind of value lives at the address.

#include <stdio.h>

int main(void) {
    int marks = 85;
    int *p = &marks;              /* p holds the address of marks */

    printf("marks = %d\n", marks);
    printf("*p    = %d\n", *p);   /* the value stored at that address */

    *p = 92;                      /* change marks through the pointer */
    printf("marks = %d\n", marks);
    printf("p points to marks: %s\n", p == &marks ? "yes" : "no");
    return 0;
}

Output

marks = 85
*p    = 85
marks = 92
p points to marks: yes
SyntaxRead it as
int *p;p is a pointer to an int
p = &x;store the address of x in p
*pthe int that p points to (dereference)
*p = 7;store 7 in the int that p points to
int *p, q;p is a pointer, but q is a plain int

The * means two different things. In a declaration (int *p) it says "p is a pointer". In an expression (*p) it means "go to the address in p". To print an address itself, use printf("%p", (void *) p); the number changes every time the program runs.

NULL pointers

A pointer that points nowhere should hold NULL. Functions often return NULL to say "not found", and you check for it before you dereference.

#include <stdio.h>

/* Returns a pointer to the first even number, or NULL when there is none */
int *first_even(int *a, int n) {
    for (int i = 0; i < n; i++)
        if (a[i] % 2 == 0)
            return &a[i];
    return NULL;
}

int main(void) {
    int odd[] = {1, 3, 5};
    int mix[] = {7, 4, 9};
    int *p = first_even(odd, 3);
    int *q = first_even(mix, 3);

    if (p == NULL) printf("no even number in odd[]\n");
    if (q != NULL) printf("first even number in mix[] is %d\n", *q);
    return 0;
}

Output

no even number in odd[]
first even number in mix[] is 4

Never dereference NULL or an uninitialised pointer. int *p; *p = 5; writes to a random address. Give every pointer a value when you declare it – a real address or NULL.

Pointer arithmetic

Adding 1 to a pointer moves it to the next element, not the next byte. If p is an int * holding 1000 and an int takes 4 bytes, p + 1 is 1004. Subtracting two pointers into the same array gives the number of elements between them.

#include <stdio.h>

int main(void) {
    int a[4] = {10, 20, 30, 40};
    int *p = a;                   /* same as &a[0] */

    for (int i = 0; i < 4; i++)
        printf("*(p + %d) = %d   a[%d] = %d\n", i, *(p + i), i, a[i]);

    printf("elements between a[3] and a[0]: %td\n", &a[3] - &a[0]);
    return 0;
}

Output

*(p + 0) = 10   a[0] = 10
*(p + 1) = 20   a[1] = 20
*(p + 2) = 30   a[2] = 30
*(p + 3) = 40   a[3] = 40
elements between a[3] and a[0]: 3

Allowed: pointer ± integer, ++ / --, pointer − pointer (same array) and comparisons such as p < a + n. Not allowed: adding two pointers, or multiplying and dividing them.

Pointers and arrays

In most expressions an array name turns into a pointer to its first element, so a means &a[0], and a[i] is exactly *(a + i). The difference: an array is a block of memory, and you cannot assign a new address to it; a pointer is a variable that you can point anywhere. Also, sizeof a gives the size of the whole array, while sizeof p gives the size of the pointer (8 bytes on a 64-bit system).

When you pass an array to a function, only the address of its first element is passed. That is why functions take the length as a separate parameter, as in void sort(int *a, int n).

Call by reference with pointers

C always passes arguments by value: a function gets copies. To let a function change the caller's variables, pass their addresses. This is how scanf("%d", &n) works.

#include <stdio.h>

void swap(int *x, int *y) {
    int t = *x;
    *x = *y;
    *y = t;
}

int main(void) {
    int a = 5, b = 9;
    swap(&a, &b);                 /* pass the addresses, not the values */
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

Output

a = 9, b = 5

Pointer to pointer

A pointer is a variable too, so it has an address. int **pp stores the address of an int *. You meet this with char **argv, 2D dynamic arrays and functions that must change a pointer, such as inserting at the head of a linked list.

#include <stdio.h>

int main(void) {
    int n = 7;
    int *p = &n;
    int **pp = &p;                /* a pointer to a pointer */

    **pp = 70;                    /* follow two arrows: pp → p → n */
    printf("n = %d, *p = %d, **pp = %d\n", n, *p, **pp);
    return 0;
}

Output

n = 70, *p = 70, **pp = 70

const pointers and void pointers

DeclarationChange *p?Change p?
const int *p (pointer to const)NoYes
int *const p (const pointer)YesNo
const int *const pNoNo

Read the declaration from right to left: int *const p is "p is a const pointer to int". A void * is a generic pointer: it can hold the address of any object (that is what malloc() returns), but you must convert it to a real type, such as *(int *) vp, before you dereference it.

Common pointer mistakes

  • Uninitialised (wild) pointer – int *p; *p = 1; writes to an unknown address.
  • Dangling pointer – using memory after free(), or returning the address of a local variable.
  • Memory leak – losing the last pointer to memory from malloc() without freeing it.
  • Out of bounds – moving a pointer past the end of an array and reading through it.
  • Modifying a string literal – char *s = "hi"; s[0] = 'H'; is undefined; use char s[] = "hi";.

Key points to remember

  • &x gives the address of x; *p gives the value at the address in p.
  • Every pointer has a type; p + 1 moves by sizeof(*p) bytes.
  • a[i] == *(a + i), and an array passed to a function becomes a pointer.
  • All data pointers are the same size on a given system (8 bytes on 64-bit).
  • Initialise pointers, check for NULL, free what you allocate, and set freed pointers to NULL.

Frequently asked questions

Why are pointers used in C?

To change variables inside functions, to work with arrays and strings efficiently, to allocate memory at run time, and to build data structures such as linked lists and trees.

What is the size of a pointer in C?

It depends on the system, not on the type it points to: 8 bytes on a 64-bit system and 4 bytes on a 32-bit system.

What is the difference between NULL and a dangling pointer?

A NULL pointer deliberately points nowhere and can be tested with p == NULL. A dangling pointer still holds an old address whose memory is no longer valid, and nothing in the pointer tells you so.

Where can I learn pointers step by step?

Read our Pointers in C Explained, Pointer Arithmetic and Function Pointers lessons, then practise with the exercises below.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C Programming

C programming is the language most computer science courses start with and the foundation of operating systems, embedded devices and compilers. This section helps you learn C properly – data types, operators, control statements, functions, arrays, strings, pointers, structures, memory allocation and files – and then prove it with hundreds of practice questions explained line by line.

Why it matters

  • C teaches how a computer really works: memory, addresses and how data is stored
  • Pointers and memory questions are a favourite in written tests and technical interviews
  • Once you know C, learning C++, Java and Python becomes much easier
  • Embedded systems, operating systems and many companies' coding rounds still use C

Who it is for

  • School students (Class 11–12 computer science) building a strong programming base
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, aptitude-cum-coding tests and technical interviews
  • Anyone preparing for GATE CS or government IT exams

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro tests
  • Technical interviews
  • GATE CS
  • University exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links