Skip to main content

Introduction to Pointers in C

 Introduction to Pointers

Introduction: In C programming, a pointer is a special variable that stores the memory address of another variable. Pointers are powerful tools that allow direct manipulation of memory and enable dynamic memory allocation. They play a crucial role in tasks such as dynamic memory allocation, accessing hardware, implementing data structures, and optimizing code performance.

In simple terms pointers are the variables which store addresses of another variable.
It can also store addresses of functions, arrays, structures and other pointers along with primitive data type variables.
But the question arises “why do we need pointers?“.
Let’s find out the answer for the above question.

Declaration of pointers

Declaration of pointer variables is almost same as declaring normal variable, only difference is that we add ‘* (asterisk)‘ sign before the pointer variable.
Syntax
data_type *pointer_variable_name;
E.g.
int *num; // pointer variable which can store address of int data type variable.
char *a; // pointer variable which can store address of char data type variable.

Size of the pointer is dependent on the architecture.

Types of Pointers:

  1. 1. Null Pointer: A pointer that does not point to any memory location. It is typically represented by the constant NULL and is often used to signify that a pointer does not currently point to a valid memory location.


  2. 2. Void Pointer (or Generic Pointer): A pointer that has no specific data type associated with it. It can be used to store the address of any data type. However, it cannot be dereferenced directly; it must be explicitly typecasted before dereferencing.


  3. 3. Pointer to Pointer (or Double Pointer): A pointer that points to another pointer. It is used in scenarios where multiple levels of indirection are needed, such as dynamic memory allocation and handling of multi-dimensional arrays.


  4. 4. Function Pointer: A pointer that points to a function instead of a data variable. Function pointers are useful for implementing callback functions, function tables, and dynamic function invocation.

Examples of Pointers:

  1. Pointer Declaration and Initialization:
c
int *ptr; // Declaration of an integer pointer 
int num = 10
ptr = # // Initialization of pointer with the address of 'num'
  1. Dereferencing a Pointer:
c
int num = 10;
int *ptr = # 
printf("Value of num: %d\n", *ptr); // Output: 10




  1. Dynamic Memory Allocation using Pointers:
c
int *arr = (int *)malloc(5 * sizeof(int)); // Allocating memory for an integer array of size 5
  1. Pointer Arithmetic:
c
int arr[5] = {1, 2, 3, 4, 5}; 
int *ptr = arr; // Pointer points to the first element of the array 
ptr++; // Move the pointer to the next element 
printf("Next element: %d\n", *ptr); // Output: 2

C Code for Swapping Two Numbers using Pointers:

c
#include <stdio.h> 
void swap(int *a, int *b) 
{ int temp = *a; *a = *b; *b = temp; }
int main()
{ int num1 = 10, num2 = 20;
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
 swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2); 
return 0; }

Notes:

Pointers are fundamental concepts in C programming that allow direct manipulation of memory addresses. They provide flexibility and efficiency in memory management and data manipulation tasks. However, improper use of pointers can lead to bugs such as segmentation faults and memory leaks. It is essential to understand pointers thoroughly and practice safe pointer usage to write reliable and efficient C programs.

Comments

Popular posts from this blog

Header Files and their Examples in C

  What Is A Header File In C? A header file in C language is a text file containing definitions of functions, variables, and macros that are shared among multiple source files. It provides an interface to the variety of functions and data structures that can be used by other parts of the program. Essentially, a header file serves as a contract that specifies what features/ operations are available to the rest of the program without exposing the implementation details. Standard Header File In C & Their Uses In C, there are many standard header files, each of which has a unique collection of utilities and methods. Naturally, there are a few standard C header files that are most typically used in applications/ C code.  We have compiled a detailed explanation of all 25 standard header files in C. The <stdio.h> Header File In C The name stands for  standard input/output header , and it contains functions for standard input and output operations such as...

Structures and Union in C

  Structures and Unions Introduction: Structures and unions are composite data types in C that allow the grouping of multiple variables of different data types under a single name. They provide a way to represent complex data structures in a program, making it easier to organize and manipulate data. Definition: Structure: A structure is a collection of variables (of different data types) grouped together under a single name. Each variable within a structure is called a member or field. Structures enable the creation of custom data types to represent real-world entities. They are defined using the struct keyword. Union: A union is similar to a structure in that it also groups variables of different data types under a single name. However, unlike structures, unions allocate memory that is only as large as the largest member. This means that only one member of the union can be accessed at a time. Unions are useful when memory conservation is a concern, and only one member needs to b...

Recursive Functions in C

  Lecture Notes: Recursive Functions in C Introduction to Recursive Functions: Recursive functions are functions that call themselves directly or indirectly to solve a problem by dividing it into smaller instances of the same problem. They are a powerful programming technique used to solve problems that can be broken down into simpler subproblems. Definition: A recursive function is a function that calls itself either directly or indirectly during its execution. Types of Recursive Functions: Direct Recursion: In direct recursion, a function calls itself directly. c Copy code // Example of direct recursion void countdown ( int n) { if (n > 0 ) { printf ( "%d " , n); countdown(n - 1 ); // Recursive call } } Indirect Recursion: In indirect recursion, a function calls another function, which in turn calls the first function, creating a cycle of function calls. c Copy code // Example of indirect recursion void func1 ( int n) ; void func2 ( ...