0% found this document useful (0 votes)
41 views14 pages

Understanding Pointers in C Programming

The document provides an overview of pointers in C programming, including their declaration, initialization, types, and usage in function arguments. It explains how pointers store memory addresses, the significance of different pointer types such as NULL, void, wild, and dangling pointers, and discusses the advantages and disadvantages of using pointers. Additionally, it includes code examples to illustrate the concepts and a question bank for further understanding of pointers.

Uploaded by

kbraju788
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views14 pages

Understanding Pointers in C Programming

The document provides an overview of pointers in C programming, including their declaration, initialization, types, and usage in function arguments. It explains how pointers store memory addresses, the significance of different pointer types such as NULL, void, wild, and dangling pointers, and discusses the advantages and disadvantages of using pointers. Additionally, it includes code examples to illustrate the concepts and a question bank for further understanding of pointers.

Uploaded by

kbraju788
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

POINTERS

Introduction
Declaration & Initialization of Pointers
Types of Pointers
Passing arguments using Pointers
A pointer is a variable that stores the memory address of another variable.
Instead of holding a direct value.

It has the address where the value is stored in memory.

This allows us to manipulate the data stored at a specific memory location without
actually using its variable.

How to use pointers in C?

First, we should define a


pointer variable.

Then, assign the address of a variable


to that pointer using & symbol.
it will return the address of that variable.

We can access the value stored in


that address by using *(asterisk) symbol.
Declaration of Pointers:

A pointer is declared by specifying its name and type, just like simple variable declaration but
with an asterisk (*) symbol added before the pointer's name.

data_type * name

Here, data_type defines the type of data that the pointer is pointing to. An integer type pointer
can only point to an integer. Similarly, a pointer of float type can point to a floating-point data,
and so on.

int *X;

In the above statement, pointer X can store the address of an integer. It is pronounced as
pointer to integer.
Initialization of Pointer

Pointer initialization means assigning some address to the pointer variable.

In C, the (&) addressof operator is used to get the memory address of any
variable. This memory address is then stored in a pointer variable.

int num = 10;


int *ptr = #

Null Pointer Initialization


A null pointer is a pointer that does not point to any valid memory location. It is
often used to indicate that a pointer is not currently in use or that an error has
occurred.

int *ptr = NULL;

or

int *ptr = 0;
#include <stdio.h>

void main()
{
int a = 10, *ptr;

ptr = &a;

printf("%d", *ptr);

Output

10
Types of Pointers

There are 4 special types of pointers that used or referred to in different contexts:

1. NULL Pointer
The NULL Pointers are those pointers that do not point to any memory location. They can
be created by assigning NULL value to the pointer.

#include <stdio.h>
void main()
{
int *ptr = NULL;
}

2. Void Pointer
The void pointers in C are the pointers of type void. It means that they do not have any
associated data type. They are also called generic pointers as they can point to any type
and can be typecasted to any type.

#include <stdio.h>
void main()
{
void *ptr;
}
3. Wild Pointers

The wild pointers are pointers that have not been initialized with something yet.
These types of C-pointers can cause problems in our programs and can eventually
cause them to crash. If values are updated using wild pointers, they could cause data
abort or data corruption.

#include <stdio.h>
​void main()
{​
int *ptr;
}

4. Dangling Pointer
A pointer pointing to a memory location that has been deleted (or freed) is
called a dangling pointer. Such a situation can lead to unexpected behavior in
the program and also serve as a source of bugs in C programs.
Passing Arguments using Pointers

In C programming, pointers can be used as function arguments, allowing functions to modify


variables directly in the calling function. This is different from passing by value, where a copy
of the variable is passed, and changes within the function do not affect the original variable.

Declaration:
In the function definition, we declare the parameter as a pointer using the * operator.
For example, void myFunction(int *ptr) indicates that the function accepts a pointer to an
integer.

Passing the Address:


When calling the function, we pass the memory address of the variable using the &
operator.

For example, myFunction(&myVariable).

Accessing the Value:


Inside the function, we use the * operator to dereference the pointer and access the value
at the memory address it points to.

For example, *ptr = 10 modifies the original variable passed to the function.
#include <stdio.h>

void modifyValue(int *ptr)


{
*ptr = *ptr + 2;
}

int main()
{
int num = 5;
printf("Before: %d\n", num);
modifyValue(&num);
printf("After: %d\n", num);
return 0;
}

In this example, the modifyValue function takes a pointer to an integer, add it by 2,


and modifies the original variable passed from main().
Advantages of Pointers

• Pointers are used for dynamic memory allocation and deallocation.


• An Array or a structure can be accessed efficiently with pointers
• Pointers are useful for accessing memory locations.
• Pointers are used to form complex data structures such as linked lists, graphs,
trees, etc.
• Pointers reduce the length of the program and its execution time as well.

Disadvantages of Pointers

• Memory corruption can occur if an incorrect value is provided to pointers.


• Pointers are a little bit complex to understand.
• Pointers are majorly responsible for memory leaks in C.
• Accessing using pointers are comparatively slower than variables in C.
• Uninitialized pointers might cause a segmentation fault.
#include <stdio.h>

void main()
{
int *p; int var = 10;

p= &var; printf("Value of variable var is: %d", var);


printf("\nalue of variable var is: %d", *p);
printf("\nAddress of variable var is: %p", &var);
printf("\nAddress of variable var is: %p", p);
printf("\nAddress of pointer p is: %p", &p); return 0;
}

Output:

Value of variable var is: 10


Value of variable var is: 10
Address of variable var is: 0x7fff5ed98c4c
Address of variable var is: 0x7fff5ed98c4c
Address of pointer p is: 0x7fff5ed98c50
Question Bank - Pointers (Module-4)

• What are Pointers ? Mention its applications (Benefits)


• How to declare & initialize Pointers, Explain with syntax and examples.
• Explain types of Pointers.
• How arguments can be passed to functions using Pointers? Explain with example
program.
• List out the drawbacks of Pointers.

You might also like