0% found this document useful (0 votes)
7 views

Introduction To Pointers in C

vbxcvbxcbxcvbc

Uploaded by

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

Introduction To Pointers in C

vbxcvbxcbxcvbc

Uploaded by

rajanikanthmeka4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction to

Pointers in C
Pointers are fundamental to the C programming language. They
allow you to directly manipulate memory addresses, providing
efficient access to data and control over memory management.

by Rajanikanth Meka
Understanding Memory
Addresses
Each byte of memory has a unique address, like a postal code for your
data. Pointers are like envelopes that store these addresses. They point
to specific locations in memory, allowing you to access and modify the
data stored there.

Physical Addresses Logical Addresses


These addresses are assigned These addresses are used by
by the hardware and identify the operating system and are
specific locations on memory mapped to physical
chips. addresses.

Virtual Addresses
These addresses are used by programs and are translated into
physical addresses by the operating system.
Declaring and Initializing
Pointers
Declaring a pointer involves specifying its data type and using the
asterisk (*) symbol. Initializing a pointer sets its value to a specific
memory address. You can assign a pointer to the address of a
variable using the ampersand (&) operator.

1 Data Type 2 Asterisk (*)


The data type of the This symbol indicates
pointer determines the that the variable is a
type of data it can point pointer.
to, such as integers,
floats, or characters.

3 Ampersand (&)
This operator returns the memory address of a variable.
Dereferencing Pointers
Dereferencing a pointer means accessing the data stored at the address
it points to. This is done by placing an asterisk (*) in front of the pointer
variable.

Pointer
The pointer variable holds the memory address of the data.

Dereference
The asterisk (*) operator accesses the data at the address.

Value
The value stored at the memory address is retrieved.
Pointer Arithmetic
Pointers can be incremented or decremented to move them to
adjacent memory locations. Each increment or decrement is based
on the size of the data type the pointer points to.

Operator Meaning

++ Increment the pointer by


one unit of its data type.

-- Decrement the pointer by


one unit of its data type.

+= Add a specified value to the


pointer.

-= Subtract a specified value


from the pointer.
Pointers and Arrays
Arrays in C are stored contiguously in memory, and pointers can be used to access individual elements. The name of
an array acts as a pointer to its first element.

Array Pointer Accessing Elements

A collection of elements of the A pointer variable that can hold Pointers can be used to access
same data type, stored the address of the first element of individual elements of the array by
sequentially in memory. the array. incrementing or decrementing the
pointer.
Pointers and Strings
Strings in C are arrays of characters, terminated by a null character ( '\0' ). Pointers
are often used to manipulate and process strings.

String Declaration
A string is declared as an array of characters.

Pointer to String
A pointer variable can point to the first character of the string.

String Manipulation
Pointers can be used to access and modify individual characters within the string.
Pointers and Functions
Functions can receive pointers as arguments and can return pointers. This
allows functions to modify data stored at specific memory locations.

1 Passing Pointers
Pointers can be passed as arguments to functions, allowing
functions to modify data outside their own scope.

2 Returning Pointers
Functions can return pointers to dynamically allocated memory
or to data structures within their scope.

3 Memory Management
Pointers are crucial for managing memory allocation and
deallocation in C.
Dynamic Memory Allocation with Pointers
Dynamic memory allocation allows you to allocate memory during program execution, using functions like malloc(),
calloc(), and realloc(). Pointers are essential for managing these dynamically allocated blocks of memory.

malloc() calloc() realloc()


Allocates a block of memory of a Allocates a block of memory and Resizes a previously allocated block
specified size and returns a pointer initializes all the bytes to zero. of memory.
to the allocated memory.
Common Pointer Pitfalls and
Best Practices
Pointers can be tricky to work with, and errors can lead to unexpected
behavior. Understanding common pitfalls and best practices is crucial for safe
and efficient pointer usage.

1 Null Pointers 2 Dangling Pointers


Always check for null pointers Avoid using pointers to
before dereferencing them, as memory that has been
this can cause segmentation deallocated. These are known
faults. as dangling pointers and can
lead to unpredictable
behavior.

3 Memory Leaks
Make sure to free dynamically allocated memory using free() to prevent
memory leaks. Failure to do so can lead to resource exhaustion and
program crashes.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy