Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Pointers in C

Rating
-
Sold
-
Pages
16
Uploaded on
16-01-2025
Written in
2022/2023

Pointers in C explains how pointers provide direct access to memory and enhance program efficiency. It covers pointer declaration, initialization, dereferencing, and arithmetic operations. The document also introduces dynamic memory allocation, explaining functions like malloc(), calloc(), realloc(), and free() to manage memory efficiently. It highlights common issues such as memory leaks and dangling pointers, providing best practices for safe memory management. With examples, code snippets, and practical applications like dynamic arrays and linked lists, the document helps learners understand the importance of pointers and dynamic memory in C programming.

Show more Read less
Institution
Course

Content preview

UNIT IV Pointers


Pointers: Pointers are one of the derived types in C. some of the advantages of pointers are
given below:
• Pointers provide direct access to memory
• Pointers provide a way to return more than one value to the functions
• Pointers provide efficient techniques for manipulating data in arrays
• Pointers can be used to pass information back and forth between the calling function and
called function
• Pointers allows us to perform dynamic memory allocation and deallocation
• Pointers help us to build complex data structures like linked list, stack, queues, trees, and
graphs etc.
Indirection operator (*): It is called as „Value at address‟ operator. It returns the value stored at
a particular address. It is also known as Dereferencing Operator
Address operator (&): The address operator (&) extracts the address for a variable. The address
operator format is as follows. &variablename;
Ex: to know the address of the variable n, just use &n
The format specifier of address is %u(unsigned integer). We can also use %x to know the
address of a variable.
The & operator can be used only with a simple variable or with an element of the array. If x is an
array then expressions such as &x[0], &x[i+3] are valid and represent the addresses of 0th and
(i+3) th elements of x.
Pointer variable: A variable which holds the address of some other variable is called pointer
variable. A pointer variable should always contain the address only.
Declaring a pointer variable In C, every variable must be declared before they are used. Since
the pointer variables contain address that belongs to a separate data type, they must be declared
as pointers before we use them.
The syntax for declaring a pointer variable is as follows

data type *ptr_name;

For example, int *p; declares the variable p as a pointer variable that points to an integer data
type. Remember that the type int refers to the data type of the variable being pointed by p



PSC VVIT, CSE Dept Page 1

,UNIT IV Pointers


Initializing Pointers: Once a pointer variable has been declared, it can be made to point to a
variable using statement such as

ptr_name=&var;

The above statement causes ptr_name to point to var. Now ptr_name contains the address of
var. This is known as pointer initialization. Before a pointer is initialized it should not be used.
Access the value of a variable using pointer variable: Once a pointer variable has been
assigned the address of a variable, we can access the value of a variable using the pointer. This is
done by using the indirection operator(*) by writing as

*ptr_name
1) Meaning of following simple pointer declaration and definition:
int a=5;
int * ptr;
ptr=&a;
Explanation:
About variable a:
1. Name of variable: a
2. Value of variable which it keeps: 5
3. Address where it has stored in memory: 1025 (assume)
About variable ptr:
4. Name of variable: ptr
5. Value of variable which it keeps: 1025
6. Address where it has stored in memory: 5000 (assume)
Pictorial representation:




PSC VVIT, CSE Dept Page 2

, UNIT IV Pointers




Pointer arithmetic or Address arithmetic: The following are the operations that are possible
with the pointers.
1. A pointer can be assigned to another pointer. This will cause both the pointers point to the
same object.
2. A pointer can be incremented or decremented. Each time a pointer is incremented it points to
the memory location of the next element of the base type. Each time a pointer is decremented
it points to the memory location of the previous element of the base type.
3. An integer can be added to or subtracted from a pointer.
4. One pointer can be subtracted from another pointer provided that they are pointing to the
same array. The expression returns the number of elements between them.




PSC VVIT, CSE Dept Page 3

Written for

Institution
Course

Document information

Uploaded on
January 16, 2025
Number of pages
16
Written in
2022/2023
Type
Class notes
Professor(s)
Vijaya anandaratnam
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
srilathalakshmisetty04

Also available in package deal

Get to know the seller

Seller avatar
srilathalakshmisetty04 Vasireddy Venkatadri Institute of Technology
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
5
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions