C Program to Demonstrate Pointers with Example
Learn C step by step.
Explore blog under C language - Pointers
Jan 22, 2026
## Chapter 9: Pointers
There are some topics in the C language that scare beginners β Pointers are at the top of that list. But the truth is, if you understand pointers correctly, half the power of C becomes clear automatically.
What is a Pointer? (Simple Human Language)
A pointer is not some magical thing. In simple terms: A pointer is a variable that stores the memory address of another variable.
That is:
* A normal variable β stores a value
* A pointer variable β stores an address
Let's understand with a Real-Life Example:
Imagine:
* Your house β the actual data
* The address of your house β the memory address
Now:
* House = value
* House address = address
* The paper on which you write down that address = pointer
The pointer itself is not the house, the pointer only tells you where the house is.
Why is the Memory Address Important?
Inside the computer:
* Every variable is stored somewhere in memory
* That location has a unique address
The pointer holds onto that address, and accesses the value through it.
How to Imagine a Pointer?
Suppose:
* Number = 10
* Address = 2001
So:
* number β holds 10
* pointer β holds 2001
And by going to 2001: We get 10 again.
Basic Pointer Syntax
Now that the idea of ββa pointer is clear, let's see how a pointer is actually written in code and what * and & do.
Practical Example: Simple Pointer Program
```c
#include
int main() {
int score = 40; // normal integer variable
int *addressPtr; // pointer of integer type
addressPtr = &score; // store the memory address of score in the pointer
printf("Value of score: %dn", score);
printf("Address of score: %pn", &score);
printf("Address stored in addressPtr: %pn", addressPtr);
printf("Value obtained from the pointer: %dn", *addressPtr); // dereferencing
return 0;
}
```
Now let's understand this in simple terms:
1. Normal variable: `int score = 40;`
* A variable named `score`
* It stores the value (40)
2. Pointer declaration: `int *addressPtr;`
* The `*` indicates that this is a pointer variable
* `int` indicates that it will store the address of data of type `int`
A pointer is always of the same data type as the data whose address it is storing.
3. Storing the address (&): `addressPtr = &score;`
* `&score` β gives the memory address of `score`
* That address is then stored in the pointer
Remember: `&` = address operator
4. Printing the pointer's value: `printf("%pn", addressPtr);`
* `%p` β used to print an address
* The output will show a hexadecimal value
5. Dereferencing (*): `*addressPtr`
* `*` here is the dereference operator
* Meaning: βGo to the address that the pointer is pointing to, and retrieve the value thereβ
Therefore:
* `addressPtr` β address
* `*addressPtr` β the value at that address
Difference between & and * (Most Important)
| Symbol | Meaning |
| :--- | :--- |
| `&` | Retrieves the address of a variable |
| `*` | Retrieves the value from an address |
**Output (Addresses will vary):**
Understanding the Output (Pointer Program Output)
Note: Memory addresses are different on every system, therefore, an exact address match is not necessary.
Sample Output:
```
Value of age: 25
Address of age: 0x7ffd5e8b9a4c
Value of ptr: 0x7ffd5e8b9a4c
Value pointed to by ptr: 25
```
Now let's understand it line by line:
1. Value of age: 25: `printf("Value of age: %dn", age);`
* `age` is a normal integer variable. It directly stores the value 25.
2. Address of age: 0x7ffd5e8b9a4c: `printf("Address of age: %pn", &age);`
* `&age` β gives the memory address of `age`. The address is in hexadecimal form (0x...).
3. Value of ptr: 0x7ffd5e8b9a4c: `printf("Value of ptr: %pn", ptr);`
* `ptr` stores an address, not a value. Because we previously wrote: `ptr = &age;`.
4. Value pointed to by ptr: 25: `printf("Value pointed to by ptr: %dn", *ptr);`
* `*ptr` β dereferences the pointer. Meaning: βGo to the address that `ptr` is pointing to, and retrieve the value there.β
Pointer Operators
To understand pointers, you only need to remember two operators:
1. & (Ampersand Operator) - Address-of Operator
* Gives the memory address of a variable.
Example: `&age` (Meaning: Where is `age` stored in memory?)
2. * (Asterisk Operator) - Dereference Operator
* Retrieves the actual value by going to the address.
Example: `*ptr` (Meaning: What value is stored at the address that ptr is pointing to?)
A simple way to remember:
* & β Give the address
* * β Give the value from the address
Modifying Values ββThrough Pointers
So far we have seen:
* A pointer holds an address
* The dereference operator (*) gives the value
Now let's understand the most powerful aspect: Through pointers, we can change the value of the original variable. This is why pointers are so important in C.
Practical Example: Changing Value using Pointers
```c
#include
int main() {
int marks = 35; // normal variable
int *ref = &marks; // address of marks in the pointer
printf("Before: marks = %dn", marks);
*ref = 60; // value changed through the pointer
printf("After: marks = %dn", marks);
return 0;
}
```
Now understand this in the simplest terms:
1. What is there initially? `int marks = 35;` Value inside marks = 35. `int *ref = &marks;` Inside ref β the address of marks.
2. How was the value changed using the pointer? `*ref = 60;` This means: The value stored at the address that ref is pointing to, should be changed to 60.
3. Why did the original variable change? `printf("After: marks = %dn", marks);` Output: marks = 60. Because: the pointer went directly to the memory location and changed the value there.
Sample Output
```
Before: marks = 35
After: marks = 60
```
Why is this concept so important? Because:
* You need to change a value inside a function
* You don't want to copy large amounts of data
* You want to write a memory-efficient program
Values ββare modified using pointers everywhere.
Remember in one line:
Change value with a normal variable β direct
Change value with a pointer β indirect but powerful
Common Beginner Mistake
Incorrect: `ref = 60;` // Assigning a value to the pointer (incorrect)
Correct: `*ref = 60;` // Changing the value at the address
Pointers and Arrays
In the C language, arrays and pointers are deeply interconnected. In fact, the array name itself is the address of its first element.
That's why it's said: Array name behaves like a pointer
What does Array Name mean? Let's say we have an array: `int list[4] = {5, 10, 15, 20};`
Here:
* list β address of the first element
* &list[0] β address of the first element
Both point to the same memory location.
Practical Example: Accessing an Array using Pointers
```c
#include
int main() {
int values[5] = {12, 24, 36, 48, 60};
int *walker = values; // values ββ= &values[0]
// Accessing elements using pointer arithmetic
for(int step = 0; step < 5; step++) {
printf("values[%d] = %dn", step, *(walker + step));
}
return 0;
}
```
Now let's understand this in simple terms:
1. Pointer assignment: `int *walker = values;`
Meaning: `values` βββ address of the first element. That same address is assigned to the pointer `walker`.
2. What is pointer arithmetic doing? `*(walker + step)` This means:
* `walker + step` β Go to the address of the next element
* `*` β Retrieve the value stored there
This is why array elements can be accessed using pointers.
How does pointer arithmetic work? If: walker = 1000, int = 4 bytes. Then: walker + 1 = 1004, walker + 2 = 1008. The pointer moves according to the size of the data type.
The most important thing to remember:
* arr[i] and *(arr + i) are the same thing.
* The only difference is the way they are written.
Pointers and Functions
Now a very important question arises: Why do we use & in scanf()? How does the value of a variable change inside a function?
The answer to both these questions lies in pointers + functions.
Core Idea (In Simple Language)
* Normally, when passing values ββto a function, a copy is made (call by value)
* When passing pointers, the actual memory address is passed
* Receiving the address means: the function can directly modify the original variable
Practical Example: Swap Function
```c
#include
// Function to swap the values ββof two variables
void interchange(int *first, int *second) {
int hold;
hold = *first; // value of first stored in hold
*first = *second; // value of second stored in first
*second = hold; // value of hold stored in second
}
int main() {
int a = 7, b = 14;
printf("Before swap: a = %d, b = %dn", a, b);
interchange(&a, &b); // addresses are passed
printf("After swap: a = %d, b = %dn", a, b);
return 0;
}
```
Let's understand it step-by-step:
1. Function declaration: `void interchange(int *first, int *second)`
* The function is taking addresses, not values. first and second β are pointers.
2. Passing the address: `interchange(&a, &b);`
* `&a` β address of a, `&b` β address of b. Therefore, the function gets direct memory access.
3. Changing the value using dereferencing: `*first = *second;`
Meaning: At the address that first is pointing to, put the value of second.
Sample Output
```
Before swap: a = 7, b = 14
After swap: a = 14, b = 7
```
It is clear from the output: The change inside the function affected the variables in main.
Why do we use & in scanf()?
Because: `scanf()` needs the address so that it can: go to that address and store the user's input value. If you don't use &: `scanf()` gets the wrong location, and the program might crash.
Call by Value vs Call by Reference (Quick View)
| Call by Value | Call by Reference |
| :--- | :--- |
| A copy is sent | The address is sent |
| Original value does not change | Original value changes |
| No pointer required | Pointer is necessary |
---
## Conclusion
In this article, we explored the core concepts of Explore blog under C language - Pointers. Understanding these fundamentals is crucial for any developer looking to master this topic.
## Frequently Asked Questions (FAQs)
**Q: What is Explore blog under C language - Pointers?**
A: Explore blog under C language - Pointers is a fundamental concept in this programming language/topic that allows developers to perform specific tasks efficiently.
**Q: Why is Explore blog under C language - Pointers important?**
A: It helps in organizing code, improving performance, and implementing complex logic in a structured way.
**Q: How to get started with Explore blog under C language - Pointers?**
A: You can start by practicing the basic syntax and examples provided in this tutorial.
**Q: Are there any prerequisites for Explore blog under C language - Pointers?**
A: Basic knowledge of programming logic and syntax is recommended.
**Q: Can Explore blog under C language - Pointers be used in real-world projects?**
A: Yes, it is widely used in enterprise-level applications and software development.
**Q: Where can I find more examples of Explore blog under C language - Pointers?**
A: You can check our blog section for more advanced tutorials and use cases.
**Q: Is Explore blog under C language - Pointers suitable for beginners?**
A: Yes, our guide is designed to be beginner-friendly with clear explanations.
**Q: How does Explore blog under C language - Pointers improve code quality?**
A: By providing a standardized way to handle logic, it makes code more readable and maintainable.
**Q: What are common mistakes when using Explore blog under C language - Pointers?**
A: Common mistakes include incorrect syntax usage and not following best practices, which we've covered here.
**Q: Does this tutorial cover advanced Explore blog under C language - Pointers?**
A: This article covers the essentials; stay tuned for our advanced series on this topic.