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


Pointers in C

Pointers in C

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?

  1. Efficient Memory Usage – Allows direct memory access.

  2. Faster Execution – Accessing memory using pointers is faster than using variables.

  3. Dynamic Memory Allocation – Used in malloc(), calloc(), and free().

  4. Passing Arguments by Reference – Functions can modify variables outside their scope.

  5. Data Structures Implementation – Used in arrays, linked lists, trees, etc.


Declaring and Using Pointers

1. Pointer Declaration


int *ptr; // Pointer to an integer

Here, ptr is a pointer that stores the address of an integer variable.

2. Assigning Address to a Pointer

c
int num = 10; int *ptr = # // Assigning address of num to ptr
  • &num gives the memory address of num.

  • ptr stores that address.

3. Accessing Value Using Pointer (Dereferencing)

c
printf("%d", *ptr); // Output: 10
  • *ptr fetches the value stored at the address stored in ptr (dereferencing).


Example: Pointers in Action

c
#include <stdio.h> int main() { int num = 20; int *ptr = &num; // Pointer stores address of num printf("Value of num: %d ", num); printf("Address of num: %p ", &num); printf("Pointer value (address of num): %p ", ptr); printf("Value using pointer: %d ", *ptr); return 0; }

Output:

yaml
Value of num: 20 Address of num: 0x7ffeefbff58c Pointer value (address of num): 0x7ffeefbff58c Value using pointer: 20

Pointer Arithmetic

  • You can perform operations like +, -, ++, -- on pointers.

  • Example:

    c
    int arr[] = {10, 20, 30}; int *ptr = arr; // Pointer to array printf("%d ", *ptr); // 10 printf("%d ", *(ptr+1)); // 20 printf("%d ", *(ptr+2)); // 30
    • ptr + 1 moves the pointer to the next integer (4 bytes ahead in memory).


Pointers and Functions

Passing Pointers to Functions

When passing a pointer, functions can modify the original value.

c
#include <stdio.h> void changeValue(int *p) { *p = 50; // Modifying value at pointer address } int main() { int num = 10; changeValue(&num); printf("Modified num: %d ", num); // Output: 50 return 0; }

Here, changeValue() modifies num directly.


Dynamic Memory Allocation (malloc, calloc, free)

Pointers are used for dynamic memory allocation.

c
#include <stdio.h> #include <stdlib.h> int main() { int *ptr = (int *)malloc(sizeof(int)); // Allocating memory *ptr = 25; printf("Value: %d ", *ptr); // Output: 25 free(ptr); // Freeing allocated memory return 0; }
  • malloc() allocates memory.

  • free() releases memory.


Key Points to Remember

ConceptDescription
*Dereference operator (access value)
&Address-of operator (get address)
ptr + 1Moves 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

c
#include <stdio.h> int main() { int num = 10; // Normal variable int *ptr; // Pointer declaration ptr = &num; // Storing address of num in ptr printf("Value of num: %d ", num); printf("Address of num: %p ", &num); printf("Value stored in ptr (Address of num): %p ", ptr); printf("Value at address stored in ptr: %d ", *ptr); return 0; }

Expected Output:

yaml
Value of num: 10 Address of num: 0x7ffeefbff5c4 (example address) Value stored in ptr (Address of num): 0x7ffeefbff5c4 Value at address stored in ptr: 10

Explanation:

  • &num gives the address of num.

  • ptr stores the address.

  • *ptr (dereferencing) gives the value stored at that address.


2. Pointer Arithmetic

Pointers can be used in calculations to access array elements efficiently.

Example: Pointer Arithmetic

c
#include <stdio.h> int main() { int arr[] = {10, 20, 30}; int *ptr = arr; // Pointer to first element of array printf("First element: %d ", *ptr); printf("Second element: %d ", *(ptr + 1)); printf("Third element: %d ", *(ptr + 2)); return 0; }

Expected Output:

yaml
First element: 10 Second element: 20 Third element: 30

Explanation:

  • ptr + 1 moves to the next array element.

  • *(ptr + 1) accesses the second element.


3. Pointers with Functions

Pointers allow pass by reference, meaning a function can modify a variable’s value.

Example: Changing Value using Pointer

c
#include <stdio.h> void updateValue(int *p) { *p = 50; // Changing value at the memory location } int main() { int num = 10; updateValue(&num); // Passing address of num printf("Updated num: %d ", num); // Output: 50 return 0; }

Expected Output:

yaml
Updated num: 50

Explanation:

  • updateValue(&num) passes the address of num to the function.

  • *p = 50 updates the actual value.


4. Dynamic Memory Allocation using Pointers

Dynamic memory allows flexible memory usage.

Example: Using malloc()

c
#include <stdio.h> #include <stdlib.h> int main() { int *ptr; ptr = (int *)malloc(sizeof(int)); // Allocating memory *ptr = 25; // Storing value printf("Value stored in allocated memory: %d ", *ptr); free(ptr); // Releasing memory return 0; }

Expected Output:

wasm
Value stored in allocated memory: 25

Explanation:

  • malloc() allocates memory.

  • *ptr = 25 stores the value.

  • free(ptr) releases memory.


5. Difference between Normal Variable and Pointer

FeatureNormal VariablePointer
StoresValueAddress of another variable
Memory AccessDirectIndirect (using *)
Exampleint num = 10;int *ptr = &num;

Possible Exam Questions (Class 12, NEB)

  1. Define a pointer. Explain with an example.

  2. Write a program to demonstrate pointer arithmetic.

  3. Write a function that modifies a variable using a pointer.

  4. What is dynamic memory allocation? Write a program using malloc().

Leave a Comment: