Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views6 pages

Pointers

Pointers in C are variables that store memory addresses of other variables, allowing for efficient memory usage and manipulation of data. They must be declared with the same type as the variable they point to and can be used to access or modify values through dereferencing. There are different types of pointers, including null, void, wild, and dangling pointers, each with specific characteristics and use cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

Pointers

Pointers in C are variables that store memory addresses of other variables, allowing for efficient memory usage and manipulation of data. They must be declared with the same type as the variable they point to and can be used to access or modify values through dereferencing. There are different types of pointers, including null, void, wild, and dangling pointers, each with specific characteristics and use cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

What Are Pointers in C Programming?

Pointers in C are variables that store the memory address of another variable
instead of holding a direct value. They let you access and manipulate data stored
in different parts of memory using that address.

This allows for efficient memory usage, dynamic memory allocation, and
powerful programming techniques like passing data by reference and creating
complex data structures such as linked lists. Pointers in C programming are
essential because they give you low-level access to how memory works.

Declaration and Initialization of Pointers in C

A pointer must be declared with the same type as the variable it will point to,
and it is initialized using the address-of operator &.

Declaration Syntax:

data_type *pointer_name;
Example:

int *ptr; // pointer to an integer


char *chPtr; // pointer to a character
float *fPtr; // pointer to a float
Initialization with Address:

int num = 10;


int *ptr = # // ptr now stores the address of num
Here, ptr points to num, meaning *ptr gives the value of num, and ptr holds its
memory address.

How to Use Pointer in C?

Using a pointer in C programming includes three basic steps: declaring it,


assigning it the address of a variable, and then accessing or modifying the value
stored at that address using the dereference operator (*):

1. Declare a Pointer

int *ptr;
This declares a pointer to an integer.

2. Assign Address to the Pointer

int num = 25;


ptr = #
Now ptr holds the memory address of the variable num.

3. Access Value Using the Pointer (Dereferencing)

printf("Value using pointer: %d\n", *ptr);


*ptr gives the value stored at the memory address ptr is pointing to — which is
25 in this case.

4. Modify Value Using the Pointer

*ptr = 100;
printf("Updated value of num: %d\n", num);
This changes the value of num to 100 via the pointer.

What Are the Different Types of Pointers?

There are majorly four types of pointers, they are:

 Null Pointer
 Void Pointer
 Wild Pointer
 Dangling Pointer

Null Pointer:

If you assign a NULL value to a pointer during its declaration, it is called Null Pointer.

Syntax:

Int *var = NULL;


Example:
#include<stdio.h>
int main()
{
int *var = NULL;
printf(“var=%d”,*var);
}

Void Pointer:
When a pointer is declared with a void keyword, then it is called a void pointer. To
print the value of this pointer, you need to typecast it.

Syntax:
void *var;
Example:
#include<stdio.h>
int main()
{
int a=2;
void *ptr;
ptr= &a;
printf("After Typecasting, a = %d", *(int *)ptr);
return 0;
}

Wild Pointer:

A wild pointer is only declared but not assigned an address of any variable. They are
very tricky, and they may cause segmentation errors.

Example:

#include<stdio.h>
int main()
{
int *ptr;
printf(“ptr=%d”,*ptr);
return 0;
}
Dangling Pointer

 Suppose there is a pointer p pointing at a variable at memory 1004. If you


deallocate this memory, then this p is called a dangling pointer.

 You can deallocate a memory using a free() function.

Example:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr=(int *)malloc(sizeof(int));
int a=5;
ptr=&a;
free(ptr);
//now this ptr is known as dangling pointer.
printf(“After deallocating its memory *ptr=%d”,*ptr);
return 0;
}

Pointer Operators in C

Pointer operators in C language allow you to work with memory addresses and
the values stored in them. Understanding these operators is essential to using
pointers effectively and safely.

1. Address-of Operator (&)

Returns the memory address of a variable.

int a = 10;
int *ptr = &a; // ptr holds the address of a
2. Dereference Operator (*)

Accesses or modifies the value stored at the memory address a pointer points to.

*ptr = 20; // updates the value of 'a' through the pointer

Relationship Between C Pointers and Arrays

The relationship between pointers and arrays in C is a fundamental concept that


reveals how closely they're connected under the hood.

Array Name as a Pointer

In most cases, the name of an array behaves like a pointer to its first element.

int arr[3] = {10, 20, 30};


int *ptr = arr; // same as int *ptr = &arr[0];

Here, ptr now points to arr[0].


Accessing Array Elements with Pointers

Array elements can be accessed using pointer arithmetic:

printf("%d", *(arr + 1)); // Output: 20


This is equivalent to:

printf("%d", arr[1]); // Output: 20


Modifying Array Elements with Pointers

*(arr + 2) = 50;
printf("%d", arr[2]); // Output: 50
You’re modifying the third element of the array using pointer notation.

Difference Between Pointers and Arrays

 An array name (arr) points to a fixed memory block; it cannot be


reassigned.
 A pointer (int *ptr) can point to different memory locations during
execution.

int a[3] = {1, 2, 3};


int *p = a;

p = p + 1; // valid
a = a + 1; // ❌ Error: array name is not assignable
Difference between arr[i] and *(arr + I)

The expressions arr[i] and *(arr + i) in C do the same thing: they both access the
i-th element of the array. But they work differently under the hood, and
understanding this difference helps you better grasp how pointers and arrays are
connected in C.

arr[i] – Array Notation

This is the most familiar and readable way to access array elements.

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


printf("%d", arr[1]); // Output: 20
*(arr + i) – Pointer Notation

This accesses the value at the memory location that is i positions ahead of the
base address.

printf("%d", *(arr + 1)); // Output: 20

You might also like