0% found this document useful (0 votes)
5 views18 pages

Pointers 2

Uploaded by

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

Pointers 2

Uploaded by

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

Pointers

Part 2
POINTERS
• Pointers play an important role when used as parameters in
function calls
• The following is an old example:

• The swap(int a, int b) function is intended to swap the values


of a and b
• However, the value of a and b do not change in main(), they
are only swapped in swap()
Take away from the example
• Passing int/float/char as parameters does not allow passing
“back” to calling function
• Any changes made to these variables are lost once the function
returns
Take away from the example
• Passing int/float/char as parameters does not allow passing
“back” to calling function
• Any changes made to these variables are lost once the function
returns
• Pointers will help us solve this problem
Changed program
Changed program

1. The function swap() uses pointer to integer arguments, int *ptra and int
*ptrb
2. The main() function calls swap(&a, &b)
3. It passes the addresses of the ints it wishes to swap
Trace the swap function

a 1 b 2

&a 0x10 &b 0x200


24 0
Trace the swap function

a 1 b 2

&a 0x10 &b 0x200


24 0

main ()

swap()
t
Trace the swap function

a 1 b 2

&a 0x10 &b 0x200


24 0

main ()

swap()
t 1
Trace the swap function

a 1 b 2

&a 0x10 &b 0x200


24 0

main ()

swap()
t 1
Trace the swap function

a 1 b 2

&a 0x10 &b 0x200


24 0

main ()

swap()
t 1
TRY: Other methods to swap
two numbers
1. XOR operator (without using temporary variable)
2. Using #define macro
3. Using Arithmetic operators (+ -) without using
temporary variable.
Solution
1. XOR operator (without using temporary variable)

2. Using #define macro

3. Using Arithmetic operators (+ -) without using


temporary variable
Visual Representation
x is an int variable
45FD 53 x that contains the
value 53
8CF2 45FD px

px is a pointer to int that


contains the address of x
y is a char variable
839B ‘H’ y that contains the
character ‘H’
8BF9 839B py

py is a pointer to char
that contains the address
of y
Pointer to a Pointer
• If we have a pointer P to some memory cell, P is also stored
somewhere in the memory
• So we can also talk about address of block that stores P.
int x=53;
45F 53 x
D
int x=53;
int *px=&x;
B4F 45F px 45F 53 x
7 D D
int x=53;
int *px=&x;
int **ppx=&px;

8BF B4F ppx B4F 45F px 45F 53 x


9 7 7 D D

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