Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

Pointers:

Pointer is a Special type of variable which contains the address of another variable. It provides a facility of direct accessing of memory locations of a variable. It also supports dynamic allocation of memory and thus improves efficiency of accessing of memory in certain functions. A Pointer is declared using the indirection operator. e.g. if a pointer ‘a‘ contains the address of an integer then it is declared as:

int *a;

e.g. If the address of x is given by &x, which is stored in p. Hence, we can write:

p=&x;


Initialization, Declaration and Accessing of Pointers:

 

Declaration of Pointers:

Declaration refers to the process of introducing the compiler to the existence of a variable or identifier. For pointers, you declare them by specifying the data type they point to, followed by an asterisk (*), and then the pointer name.

int *ptr; // Declaration of an integer pointer
char *chPtr; // Declaration of a character pointer

 

Initialization of Pointers:

Initialization is the process of assigning a value (in this case, an address) to a declared variable. Pointers are initialized by assigning them the address of another variable.

int num = 42;
ptr = # // Initialization: Assign the address of ‘num‘ to the pointer ‘ptr

You can also declare and initialize a pointer in a single step:

int num = 42;
int *ptr = # // Declaration and initialization in one step
 

Processing of Pointers:

Processing of pointers involves various operations, including dereferencing, pointer arithmetic, and dynamic memory allocation.

  • Dereferencing: Accessing the value at the memory location pointed to by a pointer using the dereference operator (*).

    printf(“Value at the memory location: %dn”, *ptr);

  • Pointer Arithmetic: Performing arithmetic operations on pointers. Adding or subtracting an integer from a pointer moves it to the next or previous memory location of the specified data type.

    int arr[5] = {1, 2, 3, 4, 5};
    int *arrPtr = arr;

    printf(“First element: %dn”, *arrPtr);
    printf(“Second element: %dn”, *(arrPtr + 1));

  • Dynamic Memory Allocation: Allocating memory dynamically using functions like malloc, calloc, or realloc.

    int *dynamicArray = (int *)malloc(5 * sizeof(int));

These operations allow you to manipulate data through pointers, access elements in arrays, and dynamically allocate and deallocate memory as needed.

In summary, declaration introduces a pointer to the compiler, initialization assigns an address to the pointer, and processing involves various operations on pointers to manipulate data in memory.
 


Importance Of Pointers: 

  1. We need Pointers to manage the memory more efficiently.
  2. We need Pointers for Efficient handling of Two-Dimensional Array.
  3. Pointers saves the memory space.
  4. Pointers used for dynamic Allocation and Deallocation of Memory Pointers.
  5. It is most Important as Pointers executes at Faster rate, so data manipulation is very fast.
  6. Pointers can be used to write compact program codes.
  7. Pointers can pass information forward and backward between functions and its reference point.