Concepts of Pointers

A pointer is a variable that stores an ADDRESS instead of a value: & asks "where does this variable live?", * asks "what lives at this address?", and that one idea explains half of C.

11 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The most feared word in C

Ask any senior about C and one word makes them sigh: pointers.

Here is the truth this lesson stands on: you have ALREADY used pointers. Every time you wrote scanf("%d", &marks), that & handed scanf an address, and scanf delivered through it.

A pointer simply makes that idea a variable: a box that stores WHERE another box lives. One picture, two operators, and the fear dissolves.

Theory

The house and the address slip

Your house is a variable: it CONTAINS things (its value). Now write your house's address on a slip of paper. The slip is a pointer: it does not contain your furniture, it contains where to find it. Hand the slip to a courier and he can deliver INTO your house without you being there. Two questions matter: "what is your address?" (&) and "what is AT this address?" (\*).

Theory

Pointers, formally

Every variable lives at a numbered memory address (the very addresses BCA103's address bus carries).

int marks = 78; /* lives at, say, address 5000 */

int *p; /* p is a POINTER to an int */

p = &marks; /* p now holds 5000 */

  • & (address-of): &marks asks "where does marks live?" → 5000.
  • *\ (dereference)*: `p` asks "what lives at the address inside p?" → 78.

Read the declaration aloud: int *p = "p points to an int". p's VALUE is an address; *p is the value AT that address.

Practical

Action at a distance

#include <stdio.h>

int main() {
    int marks = 78;
    int *p = &marks;

    printf("marks = %d\n", marks);
    printf("*p    = %d\n", *p);

    *p = 99;    /* write THROUGH the pointer */
    printf("marks is now %d\n", marks);
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Predict the output

Walk the code: p holds the address of marks, then *p = 99 writes through it. What are the three printed lines?

Show the answer

marks = 78

*p = 78

marks is now 99

The third line is the whole lesson: *writing to p changed marks itself*, because p and marks are the same box reached by two routes. Nothing was copied; the pointer is a route, not a duplicate. This action-at-a-distance is why pointers exist.

Quiz

int marks = 78; int *p = &marks; What is stored INSIDE the variable p?

  1. The memory address of marks
  2. The value 78
  3. A copy of marks that updates automatically
  4. The name "marks" as text
Show the answer

The memory address of marks

A pointer's content IS an address, nothing else: p holds where marks lives, not what it holds. The value 78 is what you get when you DEREFERENCE (*p). Students who answer 78 are merging the slip with the house, the exact confusion the analogy exists to prevent.

Quiz

After this unit's revelation, why does scanf need &marks rather than plain marks?

  1. scanf must know the ADDRESS to deliver the input into the right box
  2. The & makes scanf run faster
  3. It is only a naming convention with no meaning
  4. & converts marks into a string first
Show the answer

scanf must know the ADDRESS to deliver the input into the right box

scanf writes into your variable from inside another function, and the only way to reach a caller's box is through its address, exactly the courier with the slip. Plain marks would hand scanf a copy of 78, useless for delivery. The mysterious & of Unit 1 was a pointer all along.

Watch out

Where marks leak

Merging the two operators: & gives an address, follows one; in traces, label every step "address" or "value". Declaring wrong: `int p declares a pointer; later, bare p is the address and p` is the value, the plays two roles (declaration vs dereference) and exams test exactly that. Dangling asterisk thinking: p does not "contain marks", it contains marks's ADDRESS. Precision of language earns the marks here.

Theory

Why C keeps this power

Pointers let functions modify your variables (pass by reference, Unit 4), let programs walk arrays at speed (an array's name is secretly an address, two lessons ahead), and later, in Sem 3, let data structures grow at runtime. Every senior who sighed also aced their interviews on this exact topic. Next: the practical drill, declaring and initializing pointers of every type.

Summary

Key takeaways

  • Every variable lives at a memory address; a pointer is a variable storing an ADDRESS.
  • & = address-of (where does it live?); * = dereference (what lives there?).
  • int p = &marks: p holds marks's address; p is marks's value by another route.
  • *p = 99 changes marks itself, action at a distance.
  • scanf's & finally explained: delivery needs an address.
  • Memory hook: the house (variable) and the address slip (pointer).

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of Arrays and Pointer

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Concepts of Pointers · Computer Programming and Programming Methodology (CPPM) · Gri-Learn