TalentJR

⚙️ C programming basics · Class 10 · Lesson 2 of 18 · Free

Pointers in C – Addresses, * and &, and Changing Variables from Functions

⏱ About 35 minutes · ▶ run real C here — nothing to install · no sign-in

In this lesson you will

  • Explain what a memory address is and use & and * to work with pointers
  • Write functions that change the caller's variables by taking pointers
  • Move through an array with a pointer and check for NULL before using a pointer

A library card does not hold the book. It tells you where the book is kept: shelf 4, row 2. A pointer in C is like that card. It holds the address of a variable, so your code can reach and change the real thing from anywhere.

What is a pointer in C?

Every variable lives somewhere in memory, and each place has a number called its address. The & operator gives you a variable's address: &marks means "the address of marks".

A pointer is a variable that stores an address. int *p means "p is a pointer to an int". After p = &marks, we say p points to marks.

Putting * before a pointer follows the address and reads the value there. This is called dereferencing. So *p gives 87.

Try it: change marks to 64 and run again. *p changes too, because p only stores where marks lives.

⚙️ C — you can edit this code
Output
marks = 87
*p = 87
Does p hold the address of marks? 1
A pointer uses 4 bytes on this compiler.

💡 We do not print the address itself: it is a big number that changes from computer to computer.

How do you change a variable using a pointer?

*p can also be on the left side of =. Then you write a new value into the place p points to. The original variable changes, because it IS that place.

Here a school's fee is changed twice through the pointer. Notice we never write fee = ... directly after the start.

Try it: add the line *p = *p - 500; before the last printf and run again.

⚙️ C — you can edit this code
Output
New fee: Rs 12500
After bus fee: Rs 13500

Why does scanf need & and how do C functions change variables?

When you call a function in C, it gets COPIES of the values. Changing a copy does not change the caller's variable. This is called pass by value.

To let a function change your variable, pass its address. The function takes a pointer parameter and writes through it with *. That is exactly why scanf("%d", &n) needs the &: scanf must know where to store the number.

The classic example is swap. Two students swap their roll numbers here.

Try it: write a function void add_bonus(int *m, int bonus) that adds bonus marks, and call it with add_bonus(&a, 5).

⚙️ C — you can edit this code
Output
Before: a = 12, b = 27
After:  a = 27, b = 12

How can a C function return two answers?

return can send back only one value. With pointers, a function can fill in many answers. The caller makes empty variables and passes their addresses.

Here one function finds both the highest and the lowest run score of a batsman. The scores are in an array, which you will study fully in Lesson 3: a list of values with one name, indexed from 0.

Try it: add a score of 120 to the list, change 5 to 6, and run again.

⚙️ C — you can edit this code
Output
Highest: 88, Lowest: 3

How do pointers work with arrays in C?

An array's name works like a pointer to its first item. So int *p = days; makes p point to days[0].

Adding 1 to an int pointer moves it to the NEXT int, not the next byte. So *(p + 2) is the same as days[2]. This is called pointer arithmetic.

Subtracting two pointers into the same array tells you how many items apart they are. Here we walk through a week's attendance with a pointer.

Try it: change the loop to print only the days with 0 (absent).

⚙️ C — you can edit this code
Output
Day 1: 1
Day 2: 1
Day 3: 0
Day 4: 1
Day 5: 1
Day 6: 0
Present on 4 of 6 days
last - p = 5 items apart

What is a NULL pointer in C?

NULL is a special value that means "this pointer points to nothing". Functions often return NULL to say "not found".

You must never use * on a NULL pointer. Always check first with if (p != NULL). On a normal computer, using a NULL pointer crashes the program at once. In some places, like this browser, it may not crash, so the bug hides. Checking is the only safe habit.

This function searches roll numbers and returns a pointer to the matching marks, or NULL.

Try it: search for roll number 7 instead of 3 and run again.

⚙️ C — you can edit this code
Output
Roll 3 now has 68 marks

🤔 Predict the output

Think like C: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

int x = 10;
int *p = &x;
*p += 5;
printf("%d\n", x);

Question 2. What does this print?

int a = 3;
int b = a;
int *p = &a;
*p = 9;
printf("%d %d\n", a, b);

Question 3. What does this print?

int arr[] = {10, 20, 30, 40};
int *p = arr;
p += 2;
printf("%d %d\n", *p, *(p + 1));

🧩 Your challenge

Friends share a restaurant bill equally in whole rupees. Write void split_bill(int total, int people, int *share, int *left) that puts each person's share and the leftover rupees into the caller's variables. Read total and people with scanf. For inputs 1000 and 3 the output ends with: Each pays: Rs 333 Left over: Rs 1

Expected output (with the inputs given):
Total bill: 1000
Number of friends: 3
Each pays: Rs 333
Left over: Rs 1
⚙️ C — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

#include <stdio.h>

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

int main(void) {
    int a = 12, b = 27;
    swap(a, b);
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

Compiles and runs, but prints: a = 12, b = 27 ⏎

✅ Right

#include <stdio.h>

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

int main(void) {
    int a = 12, b = 27;
    swap(&a, &b);
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

The first swap only changes its own copies. To change the caller's variables, take pointers and pass addresses with &.

❌ Wrong

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

Line 2: error: incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int'; take the address with & [-Wint-conversion]

✅ Right

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

A pointer must hold an address, not the value 90. Use &marks to get the address.

❌ Wrong

int count = 5;
int *p = &count;
*p++;
printf("%d\n", count);

Compiles and runs, but prints: 5 ⏎

✅ Right

int count = 5;
int *p = &count;
(*p)++;
printf("%d\n", count);

++ binds tighter than *, so *p++ moves the POINTER forward and leaves count unchanged. Write (*p)++ to add 1 to the value.

📌 Remember

  • Every variable has an address; &x gives the address of x.
  • A pointer stores an address: int *p = &x; then *p reads or changes x.
  • C passes copies to functions; pass &x so a function can change x (that is why scanf needs &).
  • An array name acts like a pointer to its first item; p + 1 moves to the next item.
  • NULL means "points to nothing"; check for NULL before using * on a pointer.

Finished the quiz and the challenge?

Questions students ask

What is the difference between * and & in C?

& gives the address of a variable. * has two jobs: in a declaration like int *p it means "p is a pointer", and in an expression like *p it means "the value at that address".

Why are pointers considered hard in C?

Because mistakes do not always show at once. A pointer to the wrong place may crash the program or quietly change the wrong data. If you draw boxes and arrows on paper for each pointer, they become much easier.

Does Python or JavaScript have pointers?

Not that you can use directly. Lists and objects in those languages are passed around as references, which works a bit like a pointer behind the scenes. C lets you see and control the addresses yourself.

How big is a pointer in C?

It depends on the computer. On this browser compiler (32-bit) a pointer is 4 bytes. On most laptops today (64-bit) it is 8 bytes.