Wednesday, November 5, 2025

pointer

POINTER


 Definition :- pointer is a variable that stores the memory address of another variable. Instead of storing a value directly, a pointer stores the address of a value.

Example:

int a = 10;

int *p;      // declaration of pointer

p = &a;      // initialization

Here,

  • a → normal variable
  • p → pointer variable
  • &a → address of variable a

So, p stores the memory address of a.


Symbols Used in Pointer

Symbol

Meaning

&

Address-of operator (used to get the address of a variable)

*

Value-at operator (used to get or set the value at that address)

Example:

printf("%d", *p);   // prints value stored at address of a (i.e., 10)


Declaration and Initialization of Pointer

Declaration:

To declare a pointer, use * before the variable name.

Syntax:

data_type *pointer_name;

Example:

int *p;

float *q;

char *r;

Initialization:

Assign the address of a variable to the pointer using the & operator.

Example:

int a = 5;

int *p;

p = &a;


Program

#include <stdio.h>

 

int main() {

    int a = 10;

    int *p;

    p = &a;

 

    printf("Value of a = %d\n", a);

    printf("Address of a = %p\n", &a);

    printf("Value stored in pointer p = %p\n", p);

    printf("Value pointed by p = %d\n", *p);

 

    return 0;

}

Output:

Value of a = 10

Address of a = (some memory address)

Value stored in pointer p = (same address)

Value pointed by p = 10


Advantages of Pointers in C

1   Pointers allow direct access to memory locations, making program execution faster.

2   With pointers and functions like malloc() and free(), memory can be allocated and freed at runtime.

3   Pointers make it easy to access and manipulate array elements using pointer arithmetic.

4   Pointers allow functions to modify actual variable values by passing their addresses.

5   Pointers are essential for creating dynamic data structures like linked lists, stacks, queues, and trees.

6   Reduces data copying, saves time, and increases program efficiency.

7   Pointers are widely used for handling strings efficiently in C.

8   Enables calling functions dynamically and helps in implementing callbacks.

Disadvantages of Pointers in C

1   Pointers are difficult for beginners to learn and understand properly.

2   If memory allocated using malloc() or calloc() is not freed using free(), it causes memory wastage.

3   When a pointer refers to memory that has already been freed, it may cause program errors or crashes.

4   Using uninitialized pointers can lead to unpredictable results or system crashes.

5   Improper pointer use can overwrite memory locations, leading to data corruption or security vulnerabilities.

6   Errors caused by wrong pointer operations are difficult to find and fix.

7   Accessing invalid or NULL pointers may crash the program.


Types of Pointers in C

1. Null Pointer :-  A null pointer is a pointer that does not point to any valid memory location. It is usually assigned the value NULL to indicate that it is empty or not yet used. This helps prevent accidental use of uninitialized pointers.

Example:

int *p = NULL;

 Here, p is a null pointer because it does not point to any variable or memory address.


2. Void Pointer (Generic Pointer):- A void pointer is a special type of pointer that can hold the address of any data type — int, float, char, etc.
Since it is “generic,” it must be typecasted to the correct data type before use
.

Example:

void *ptr;

int a = 5;

ptr = &a;

printf("%d", *(int *)ptr);

 Here, ptr is a void pointer that stores the address of a.


3. Wild Pointer:- A wild pointer is a pointer that has been declared but not initialized. It points to an unknown memory location, which may cause errors or crashes when used.

Example:

int *p;   // Wild pointer

*p = 10;  //  Undefined behavior

 Always initialize pointers before using them.


4. Array Pointer:- An array pointer points to the first element of an array.
The array name itself acts as a pointer to its first element.

Example:

int arr[5] = {10, 20, 30, 40, 50};

int *p = arr;

printf("%d", *p);     // prints 10

printf("%d", *(p+2)); // prints 30

Here, p points to the first element of the array arr.


5. Function Pointer:- A function pointer stores the address of a function.
It allows calling a function using its pointer, which is useful for callback functions and dynamic program control.

Example:

#include <stdio.h>

void show() {

    printf("Hello World");

}

int main() {

    void (*p)() = show;

    p();   // calls the function show()

    return 0;

}

 Here, p is a pointer to the function show().


6. Pointer to Structure:- A pointer to structure stores the address of a structure variable. It allows us to access structure members using the arrow operator (->).

Example:

#include <stdio.h>

struct student {

    int age;

    char name[20];

};

int main() {

    struct student s1 = {15, "Ravi"};

    struct student *p = &s1;

    printf("Name: %s, Age: %d", p->name, p->age);

    return 0;

}

 Here, p is a pointer to the structure variable s1.


7. Pointer to Pointer:- A pointer to pointer means a pointer that stores the address of another pointer. It is used when we need multiple levels of referencing.

Example:

int a = 10;

int *p = &a;

int **q = &p;

printf("%d", **q);  // prints 10

 Here, q is a pointer to pointer p.


8. Constant Pointer:- A constant pointer is a pointer whose address value cannot be changed once it is assigned. However, the data at that address can still be modified.

Example:

int a = 10, b = 20;

int *const p = &a;

*p = 15;   // allowed (value can change)

p = &b;    // not allowed (address cannot change)

 Here, p always points to a.

 


No comments:

Post a Comment

ponter in hindi

  पॉइंटर (Pointer) परिभाषा (Definition): Pointer एक ऐसा वेरिएबल (variable) है जो किसी दूसरे वेरिएबल का मेमोरी एड्रेस (memory address) स...