Pointers in C
Class12 NEB -Computer Science
Exam for Computer Science
Posted by yanib on 2025-03-22 20:48:40 |
Last Updated by yanib on 2025-07-19 01:20:59
Share: Facebook |
Twitter |
Whatsapp |
Linkedin Visits: 221
A pointer in C is a variable that stores the memory address of another variable. Instead of storing a direct value, a pointer holds the location where the value is stored.
Why Use Pointers?
-
Efficient Memory Usage – Allows direct memory access.
-
Faster Execution – Accessing memory using pointers is faster than using variables.
-
Dynamic Memory Allocation – Used in malloc()
, calloc()
, and free()
.
-
Passing Arguments by Reference – Functions can modify variables outside their scope.
-
Data Structures Implementation – Used in arrays, linked lists, trees, etc.
Declaring and Using Pointers
1. Pointer Declaration
Here, ptr
is a pointer that stores the address of an integer variable.
2. Assigning Address to a Pointer
3. Accessing Value Using Pointer (Dereferencing)
Example: Pointers in Action
Output:
Pointer Arithmetic
Pointers and Functions
Passing Pointers to Functions
When passing a pointer, functions can modify the original value.
Here, changeValue()
modifies num
directly.
Dynamic Memory Allocation (malloc, calloc, free)
Pointers are used for dynamic memory allocation.
Key Points to Remember
Concept | Description |
---|
* | Dereference operator (access value) |
& | Address-of operator (get address) |
ptr + 1 | Moves to next memory location |
malloc() | Allocates memory dynamically |
free() | Releases allocated memory |
Would you like more examples or explanations? 😊
You said:
tell me easily with proper examples that might be asked in NEB examination for class 12 students in Nepal
ChatGPT said:
Pointers in C (For NEB Class 12 Exam)
A pointer in C is a special variable that stores the memory address of another variable.
1. Declaring and Using Pointers
Concept:
A pointer is declared using *
, and it stores the address of another variable using &
.
Example: Simple Pointer Program
Expected Output:
Explanation:
2. Pointer Arithmetic
Pointers can be used in calculations to access array elements efficiently.
Example: Pointer Arithmetic
Expected Output:
Explanation:
3. Pointers with Functions
Pointers allow pass by reference, meaning a function can modify a variable’s value.
Example: Changing Value using Pointer
Expected Output:
Explanation:
4. Dynamic Memory Allocation using Pointers
Dynamic memory allows flexible memory usage.
Example: Using malloc()
Expected Output:
Explanation:
-
malloc()
allocates memory.
-
*ptr = 25
stores the value.
-
free(ptr)
releases memory.
5. Difference between Normal Variable and Pointer
Feature | Normal Variable | Pointer |
---|
Stores | Value | Address of another variable |
Memory Access | Direct | Indirect (using * ) |
Example | int num = 10; | int *ptr = # |
Possible Exam Questions (Class 12, NEB)
-
Define a pointer. Explain with an example.
-
Write a program to demonstrate pointer arithmetic.
-
Write a function that modifies a variable using a pointer.
-
What is dynamic memory allocation? Write a program using malloc()
.