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 and data structures in c

Rating
-
Sold
-
Pages
22
Uploaded on
25-03-2023
Written in
2022/2023

Complete knowledge about pointers and data structures in an easy way.

Institution
Course

Content preview

Pointers

Introduction

 A pointer is a derived data type. This is the one which stores the address of data in memory, we will
be in position to access the data directly and do calculations over it.
 The standard concept is, access the data from memory using variable name it gets the data and
operations are done over them.
 But the pointer is different that the accessing is done by address the data is stored so that it will be
advantage of decreasing the instructions and overheads of standard usage.

Definition:

A pointer is a variable which contains the address of another variable.
Advantages of pointer

 Enables us to access a variable that is defined outside the function.
 Can be used to pass information back and forth between a function and its reference point.
 More efficient in handling data tables.
 Reduces the length and complexity of a program.
 Sometimes pointers also increases the execution speed.

Declaring of a pointer variable

General form:
data_type *pointer_name;
where,
• The asterisk (*) tells that the variable pointer_name is a pointer variable.
• Pointer_name is a identifier.
• pointer_name needs a memory location.
• pointer_name points to a variable of type data_type which may be int, float, double etc..
Example:




where
 ptr is not an integer variable but ptr can hold the address of the integer variable i.e. it is a pointer to an
integer variable’ ,but the declaration of pointer variable does not make them point to any location .
 We can also declare the pointer variables of any other data types .
For example:
double * dptr;
char * ch;
float * fptr;

,Dereference operator (*)
The unary operator (*) is the dereferencing pointer or indirection pointer , when applied to the
pointer can access the value the pointer points to.

Example:

int i= 15;
int * ptr;
ptr=&i;
printf(“Value of i is :%d”,i);
printf(“Address of i is :%d”,&i);
printf(“Value of i is :%d”,*ptr);
printf(“address of i is :%d”,ptr);
printf(“address of pointer is :%x”,&ptr);

Value of i is : 15
Address of i is : 1003
Value of i: 15
Address of i is : 1003
Address of ptr is : 2712

The null pointer
 Sometimes it is useful to make our pointers initialized to nothing . This is called a null pointer.
 A null pointer is the pointer does not point to any valid reference or memory address .We assign a
pointer a null value by setting it to address 0:
 Example
int *iptr;
iptr=0; or iptr = NULL;
 This statement assign address 0 to iptr . This statement describe a special preprocessor define called NULL
that evaluates to 0.
 Hence A null pointer is a pointer type which has a special value that indicate it is not pointing to any valid
reference.

Initialization of pointers
Initializing a pointer variable is a important thing, it is done as follows:
Step 1: Declare a data variable
Step 2:Declare a Pointer variable
Step 3:Assign address of data variable to pointer variable using & operator and assignment
operator.
Example:
int x;
int *p
p = &x;

, Pointer arithmetic

Pointer arithmetic operations are different from normal arithmetic operations.
The operations allowed to on Pointers are:
1. Add or subtract integers to/from a pointer. The result is a pointer.
2. Subtract two pointers to the same type. The result is an int.
3. Comparison of two pointers

Note: Multiplication, addition, division of two pointers not allowed.
1. Add or subtract integers to/from a pointer

 Simple addition or subtractions operations can be performed on pointer variables.
 If *iPtr points to an integer, *iPtr + 1 is the address of the next integer in memory after *iPtr. *iPtr
- 1 is the address of the previous integer before *iPtr
Examples:

int a=57; int a=57;
int *iptr=&a; iptr= iptr +1; iptr = iptr +(2B) iptr int *iptr=&a; iptr= iptr -1; iptr = iptr -(2B) iptr
=1003+2 =1003-2
iptr =1005 iptr =1001

Written for

Institution
Course

Document information

Uploaded on
March 25, 2023
Number of pages
22
Written in
2022/2023
Type
Class notes
Professor(s)
Dr karthik
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
hemantkumar1

Also available in package deal

Get to know the seller

Seller avatar
hemantkumar1 nmit
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
7
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Trending documents

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

Frequently asked questions