Programmer Coding

Pointer in C Language

What is Pointer?

A pointer in C is a variable that stores the address of another variable. This variable can be an int, char, array, function or any other parameter. The size of

Syntax

int n = 10;

int * p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.

the array depends on the architecture. However, on 32-bit architectures, the size of the pointer is 2 bytes.

Consider the following example to define a pointer that stores an integer address.

What Are Pointers?

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The general form of a pointer variable declaration is:

type *var-name;

Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Following are the valid pointer declaration:

int *ip; /* pointer to an integer */

double *dp; /* pointer to a double */

float *fp; /* pointer to a float */

char *ch /* pointer to a character */

The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.

How to use Pointers?

There are few important operations, which we will do with the help of pointers very frequently. (a) we define a pointer variable (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. Following example makes use of these operations:

#include <stdio.h>

int main ()

{

int var = 20; /* actual variable declaration */

int *ip; /* pointer variable declaration */

ip = &var; /* store address of var in pointer variable*/

printf(“Address of var variable: %x\n”, &var );

/* address stored in pointer variable */

printf(“Address stored in ip variable: %x\n”, ip );

/* access the value using the pointer */

printf(“Value of *ip variable: %d\n”, *ip );

return 0;

}

Output

Address of var variable: bffd8b3c

Address stored in ip variable: bffd8b3c

Value of *ip variable: 20

Declaring a pointer

The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.

  1. int*a;//pointer to int
  2. char*c;//pointer to char

Example

#include <stdio.h>

int main() {

int num = 10;

int *ptr = &num; // Pointer ptr now points to the address of num

printf(“Value of num: %d\n”, num); // Print the value of num directly

printf(“Address of num: %p\n”, &num); // Print the memory address of num

printf(“Value of num using pointer: %d\n”, *ptr); // Dereference ptr to get the value of num

printf(“Address stored in ptr: %p\n”, ptr); // Print the memory address stored in ptr

return 0;

}

Output

Value of num: 10

Address of num: 0x7ffeefbff57c

Value of num using pointer: 10

Address stored in ptr: 0x7ffeefbff57c

Pointer to array

  1. intarr[10];
  2. int*p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer array arr.

Pointer to a function

  1. voidshow (int);
  2. void(*p)(int) = &display; // Pointer p is pointing to the address of a function

Pointer to structure

  1. struct st {
  2. int i;
  3. float f;
  4. }ref;
  5. struct st *p = &ref;

Advantages of Pointer in C

 

  • Dynamic memory allocation:

    Pointer allows dynamic memory allocation using functions such as malloc, calloc, and realloc, allowing the program to allocate memory as needed at runtime. This flexibility is important for effective memory management, especially in applications where memory is not available.

  • Data efficiency:

    Pointers are useful for handling complex data such as linked lists, trees, graphs, and dynamic arrays. These data systems rely on pointers to link elements together and dynamically manage memory for efficient processing and transfer of data.

  • Passing Parameters by Reference:

    A pointer allows the function to modify the variable in the call by passing the address of the variable as a parameter. This technique, called “pass by reference,” allows functions to operate on a variable directly from the original, thus eliminating the overhead of creating copies and refining the code.

  • Array Manipulation:

    The pointer provides a simple way to operate arrays with high performance. They allow direct access to array elements and enable efficient manipulation of array elements using arithmetic operators. Additionally, the index can create multiple arrays and dynamic arrays.

  • Function Pointers:

    Function Metrics enable data-driven operations, simplifying function calls and callbacks, event handling, and multitasking. Performance indicators are widely used in C.

a to implement callbacks, timers, and programmatic programs.

  • Accessing Hardware and System Resources:

    Instructions are often used in programming and low-level tasks to access hardware registers, operate memory-mapped devices, and interact with resources. They provide direct access to memory space and enable communication with external devices.

  • String functions:

    Pointers are the basis of string functions in C. C-style strings are represented as strings of characters terminated by the null character (‘\0’). Metrics allow efficient traversal and manipulation of strings, including concatenation, copying, comparison, and tokenization.

  • Reduced Memory Overhead:

    Pointers help create data structures and processes with lower memory compared to other methods. It allows efficient memory use of data and algorithms by allowing memory sharing between multiple variables.

Address Of (&) Operator

The address of operator ‘&’ returns the address of a variable. But, we need to use %u to display the address of a variable.

  1. #include<stdio.h>
  2. intmain(){
  3. intnumber=50;
  4. printf(“value of number is %d, address of number is %u”,number,&number);
  5. return0;
  6. }

Output

value of number is 50, address of number is fff4

Null Pointer

A pointer that has no value assigned but is NULL is called a NULL pointer. If no address is specified in the pointer when declared, a NULL value may be assigned. It will provide a better way.

Pointer Program to swap two numbers without using the 3rd variable

#include <stdio.h>

int main() {

int num1 = 10;

int num2 = 20;

int *ptr1 = &num1; // Pointer to num1

int *ptr2 = &num2; // Pointer to num2

int temp; // Third variable for swapping

printf(“Before swapping:\n”);

printf(“num1 = %d, num2 = %d\n”, num1, num2);

// Swapping values using pointers and a third variable

temp = *ptr1;

*ptr1 = *ptr2;

*ptr2 = temp;

printf(“After swapping:\n”);

printf(“num1 = %d, num2 = %d\n”, num1, num2);

return 0;

}

Output

Before swapping:

num1 = 10, num2 = 20

After swapping:

num1 = 20, num2 = 10

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top