Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
16 views
Pointers Not
Uploaded by
aschandel.perso
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save pointers not For Later
Download
Save
Save pointers not For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
16 views
Pointers Not
Uploaded by
aschandel.perso
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save pointers not For Later
Carousel Previous
Carousel Next
Save
Save pointers not For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 4
Search
Fullscreen
Pointers in C Pointers are variables that used to store the addresses of other type of variables (or functions) in C. In the adjacent figure, ‘num is an int type variable with value 26 ptris a pointer holding the address of rum (the address of num is 1100 ) piritself is a variable (pointer type) stored at address 1080 Image source : hitps://eleetricalworkbook.com/e-pointers! Addresses of variables can be obtained using the & operator (known as referencing) and can be assigned to @ pointer type variable, Pointers also provide the operation of * ( dereferencing ) which is getting the value of the variable stored at the address contained inside a pointer variable. &- “Address of (Referencing ) operator, used to get the address ofa variable * = “Value at” ( De- referencing operator) , used to get the value at the address contained ina pointer type variable The following code illustrates the use of the following operators using a pointer to an int type 1} source code to store the addess of int variable Winclude
int main() ' int a= 100; int variable initialized to 100 int * p= 8a; I/ pointer to an integer variable ithe pointer p holds the address ofthe variable address printi("\n Address of ais Yeu", &a); address of address printi("In The contents of the pointer variable printi("in Value at pis %d", *p) Wend mai Scanned with CamScannerUses of Pointers * Passing arguments to functions using call by reference « To Retum multiple values from functions Passing structures and arrays to functions efficiently « Accessing array elements using base address and offset Manipulating strings © Accessing structure members ¢ Dynamic memory allocation © Creating different type of data structures like lists, trees etc. Reading and writing directly from/into some memory location « Low level programming, Systems programming Pointer Arithmetic Like other data types pointers do have some specific operations defined on them. These operations are Increment/Decrement Addition/Subtraction by integer Difference of two pointers Comparison of two pointers . Increment/Decrement of pointers If we increment a pointer by 1, the pointer will start pointing to the immediate next location. This is somewhat different from the general arithmetic since the value of the pointer will get increased by the size of the data type to which the pointer is pointing. For e.g. in a platform with 4 byte integer , a increment of I to an integer pointer will result in the pointer moving 4 bytes in memory. Similarly when a character pointer is incremented by 1 , it will move I byte in memory. The following illustrates this concept. Scanned with CamScanner17" Code to observe the increment ina pointer when incremented by 1 */ ‘nctude
fet maing) « Int rumber=50; Int *p pointer to int peAnumber/stores the address of number variable print(Address ofp varabe Is Sou or, pat print(After Increment: Adéress ofp variable Is S6u \n");// In our case, p will get Incromented by 4 bytes. return 0 > The above code will exhibit different increment values in the pointer when depending upon the type of the pointer. Addition/subtraction with an integer is similar to increment/decrement, but here the changes in the address contained in the pointer will be a multiple of the size of the data type to which the pointer is pointing. For e.g. if p is a pointer to an integer type ( int * ) then p + 5 will lead to p incremented by 5*sizeoiint) bytes. For a platform which provides 4 bytes integer type, p+ 4 will increment p by 20 bytes. Ifp is a pointer to a double type (double * ) then p +5 will lead to p incremented by 40 bytes. Difference of two pointers Two pointers can be subtracted when they point to elements of the same array object. The result of the subtraction is the number of that type that can be accommodated between the two addresses. The following code prints 100 becasse 100 integers can be accommodated between the addresses pointed by p and q. Taking the difference of tow pointers is useful in when accessing array elements with pointers. Hi source code for difference between two pointers include
int main) ( int a[100}// array of 100 integers int * p, *q// p and q are integer pointers p= &aj0]: /"p pointing to the first clement of the array &a{100];//q points to the las element ofthe array (100th ) print" ap Su", q-P), 1 prints 100 , because these addresses are seperated by 180 integers lend of main Comparison of pointers Two pointers are considered equal if they are pointing to the same memory location, ‘Types of Pointers Pointers can be of various types depending upon which data type it is pointing or which object its is pointing. We can have pointers of the following types — * Pointers to various primitive types such as int , char, float ete. + Pointers to arrays * Pointers to pointers * Pointers to derived types such as structures, unions etc. * Pointers to functions Scanned with CamScanner© void pointers ( generic type , can be typecasted to any type ) In addition to these pointers can also be classified as per the size of the address space in which they are pointing to. Acoording to this pointers can be classified into the following types © Near pointers (default) Near pointer is a pointer which is used to bit address of up to 16 bits in a given section of the computer memory that is 16 bit enabled. It can only access data of a small size of about 64 kb in a given period, which is the main disadvantage of this. © Far pointers Far pointer is a 32-bit pointer, can access information which is outside the computer memory in a given segment. To use this pointer, one must allocate his/her sector register to store data address in the segment and also another sector register must be stored within the most recent sector. © Huge pointers Huge pointer has the same size of 32-bit to that of a far pointer, and it can also access bits that are located outside the sector. Far pointer which is fixed and hence that part of the sector in which they are located cannot be modified in any way; huge pointers can be. Programs based on Pointers Q Write a C program to swap the value of 2 integer variables using pointers. M1 source code to swap two integer variables using pointers, include
int main { int x=100 , y=200 , temp; // integer variables int *p, *q;// pointers to integer type p=&x: !p points tox q=&y, [fq points toy the values of integers before swapping Yad and y=%d", x, y)s Méisplay the values of integers after swapping, printi("\n x=9%d and y=%d ", x,y): Mend of main Scanned with CamScanner
You might also like
CP Unit-4 R19
PDF
No ratings yet
CP Unit-4 R19
22 pages
Advanced C Programming Techniques
PDF
No ratings yet
Advanced C Programming Techniques
36 pages
unit-3 computer programming
PDF
No ratings yet
unit-3 computer programming
12 pages
C Pointers
PDF
No ratings yet
C Pointers
7 pages
CP Unit-4 (R-23)
PDF
No ratings yet
CP Unit-4 (R-23)
33 pages
Cpps Mod5@Azdocuments - in
PDF
No ratings yet
Cpps Mod5@Azdocuments - in
23 pages
Pointers
PDF
No ratings yet
Pointers
39 pages
RDoc38062
PDF
No ratings yet
RDoc38062
25 pages
Pointers in C
PDF
No ratings yet
Pointers in C
32 pages
08 Pointers
PDF
No ratings yet
08 Pointers
48 pages
10 Pointers
PDF
No ratings yet
10 Pointers
48 pages
7 Pointers in C
PDF
No ratings yet
7 Pointers in C
32 pages
Pointers and Introduction To Data Structures
PDF
No ratings yet
Pointers and Introduction To Data Structures
22 pages
Pointers: of An Another Variable. by Convention, Zero Represents The "Null" Pointer. Memory To Be Addressed
PDF
No ratings yet
Pointers: of An Another Variable. by Convention, Zero Represents The "Null" Pointer. Memory To Be Addressed
23 pages
Pointers in C
PDF
No ratings yet
Pointers in C
32 pages
Unit 3 of C
PDF
No ratings yet
Unit 3 of C
91 pages
IP U-IV
PDF
No ratings yet
IP U-IV
19 pages
PPS Unit 4 Notes Full
PDF
No ratings yet
PPS Unit 4 Notes Full
43 pages
Unit 6
PDF
No ratings yet
Unit 6
19 pages
C - Pointer
PDF
No ratings yet
C - Pointer
29 pages
Unit9 Pointers
PDF
No ratings yet
Unit9 Pointers
14 pages
Lecture13 Pointers Array
PDF
No ratings yet
Lecture13 Pointers Array
26 pages
Unit 7: Pointers
PDF
No ratings yet
Unit 7: Pointers
33 pages
Unit - 4
PDF
No ratings yet
Unit - 4
44 pages
Pointers
PDF
No ratings yet
Pointers
18 pages
Pointers in C - C++
PDF
No ratings yet
Pointers in C - C++
41 pages
pointers
PDF
No ratings yet
pointers
49 pages
CP II Pointers (Unit III)
PDF
No ratings yet
CP II Pointers (Unit III)
22 pages
c unit 5
PDF
No ratings yet
c unit 5
12 pages
POINTERS in C Programming Language
PDF
No ratings yet
POINTERS in C Programming Language
31 pages
Pointers
PDF
No ratings yet
Pointers
62 pages
C Pointers
PDF
No ratings yet
C Pointers
9 pages
PSUC UNIT - 5
PDF
No ratings yet
PSUC UNIT - 5
80 pages
Unit III - Pointers
PDF
No ratings yet
Unit III - Pointers
23 pages
Pointers PDF
PDF
No ratings yet
Pointers PDF
28 pages
Lec 20 Pointers in C
PDF
No ratings yet
Lec 20 Pointers in C
25 pages
Unit 4
PDF
No ratings yet
Unit 4
24 pages
3_Advanced Concepts of C Programming
PDF
No ratings yet
3_Advanced Concepts of C Programming
28 pages
WINSEM2023-24 BECE320E ETH VL2023240504752 2024-02-26 Reference-Material-I
PDF
No ratings yet
WINSEM2023-24 BECE320E ETH VL2023240504752 2024-02-26 Reference-Material-I
17 pages
Unit 4 Complete Autonomous
PDF
No ratings yet
Unit 4 Complete Autonomous
33 pages
Lecture 2 - Pointers
PDF
No ratings yet
Lecture 2 - Pointers
53 pages
Pointers
PDF
No ratings yet
Pointers
60 pages
C Pointers 1
PDF
No ratings yet
C Pointers 1
7 pages
Pointers: C&Dsforimca Unit-4 Vvit
PDF
No ratings yet
Pointers: C&Dsforimca Unit-4 Vvit
9 pages
Pci Unit6
PDF
No ratings yet
Pci Unit6
25 pages
Pointers in C
PDF
No ratings yet
Pointers in C
39 pages
Pointers
PDF
No ratings yet
Pointers
37 pages
Unit_2
PDF
No ratings yet
Unit_2
113 pages
7.Lecture 7 9pointers 1
PDF
No ratings yet
7.Lecture 7 9pointers 1
27 pages
Concept of Pointer
PDF
No ratings yet
Concept of Pointer
6 pages
Module 5
PDF
No ratings yet
Module 5
21 pages
Pointerscpp
PDF
No ratings yet
Pointerscpp
64 pages
C Day - 2 Topics Covered 1. Pointers 2. Structures 3. Unions 1.0 Introduction To Pointers
PDF
No ratings yet
C Day - 2 Topics Covered 1. Pointers 2. Structures 3. Unions 1.0 Introduction To Pointers
17 pages
Unit 3 PC
PDF
No ratings yet
Unit 3 PC
70 pages
Pointers
PDF
No ratings yet
Pointers
35 pages
1.3 - Review Pointer
PDF
No ratings yet
1.3 - Review Pointer
52 pages
Oop M03
PDF
No ratings yet
Oop M03
64 pages
Pointers
PDF
No ratings yet
Pointers
28 pages
Session 6
PDF
No ratings yet
Session 6
94 pages