Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
College aantekeningen

Pointers and data structures in c

Beoordeling
-
Verkocht
-
Pagina's
22
Geüpload op
25-03-2023
Geschreven in
2022/2023

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

Instelling
Vak

Voorbeeld van de inhoud

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

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
25 maart 2023
Aantal pagina's
22
Geschreven in
2022/2023
Type
College aantekeningen
Docent(en)
Dr karthik
Bevat
Alle colleges

Onderwerpen

€7,58
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
hemantkumar1

Ook beschikbaar in voordeelbundel

Maak kennis met de verkoper

Seller avatar
hemantkumar1 nmit
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
-
Lid sinds
3 jaar
Aantal volgers
0
Documenten
7
Laatst verkocht
-

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Populaire documenten

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Veelgestelde vragen