Nearby lessons

90 of 124

C - Dereference Pointer (*)

Dereference Pointer (*) is one of the foundational topics in C programming. This lesson explains The Two Pointer Operators, Program 1: see & and * working and Complete Program 2 — Change a Value Through a Pointer with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

The Two Pointer Operators

OperatorNameWhat it does
&Address-of operatorGives the memory address of a variable
*Indirection / dereferenceGoes to the address and gives the value stored there

Program 1: see & and * working

Trainer's Note: Memory trick: `&` reads 'address of', `*` reads 'value at'. So p = &a puts a's address in p, and *p gives back a's value. Beginners often mix them — practise this one program until it is clear.
Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int a = 10;
6 int *p = &a; // p stores a's address
7 
8 printf("Value of a : %d\n", a); // 10
9 printf("Address of a : %p\n", &a); // some memory address
10 printf("Value of p : %p\n", p); // same address
11 printf("Value at *p : %d\n", *p); // 10 - the value a holds
12}
Output
Value of a : 10 Address of a : 0x7ffe... Value of p : 0x7ffe... Value at *p : 10

Complete Program 2 — Change a Value Through a Pointer

Using *p you can read or change the value at that address:

Example03
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int a = 10;
6 int *p = &a;
7 
8 printf("Before: a = %d\n", a); // 10
9 
10 *p = 50; // change the value AT a's address
11 
12 printf("After : a = %d\n", a); // 50 - a changed through the pointer!
13}
Output
Before: a = 10 After : a = 50
📝 Key Takeaways
  • A pointer stores the address of a variable; it 'points to' it.
  • & gives the address; * gives the value at an address.
  • Each concept has its own program: & and *, change via pointer, arrays, swap, NULL.
  • Array name = address of the first element; *(p+i) walks the array.
  • Call by reference uses & and * so a function can change original variables.
  • Swap is the classic call-by-reference example.
  • Always check for NULL before using a pointer.
  • (Advanced pointer topics — pointers to functions, linked lists — are beyond beginner scope.)

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1