0% found this document useful (0 votes)
84 views9 pages

Lab 7: Pointer: Objectives

The document discusses pointers in C programming, including: - Declaring pointer variables of different data types - Using the & and * operators to get the address and value of a variable - Performing pointer arithmetic and pointer expressions - Passing pointers to functions using call by reference - Several examples are provided to demonstrate pointer operations on arrays and variables
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views9 pages

Lab 7: Pointer: Objectives

The document discusses pointers in C programming, including: - Declaring pointer variables of different data types - Using the & and * operators to get the address and value of a variable - Performing pointer arithmetic and pointer expressions - Passing pointers to functions using call by reference - Several examples are provided to demonstrate pointer operations on arrays and variables
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

CCSB163 Principles of Programming College of Information technology Universiti Tenaga Nasional

Lab 7: Pointer
Objectives:You ill un!erstan! pointer in C that inclu!es" # $eclaring pointer varia%les Pointer operations Pointer e&pressions 'pointer assignment( pointer arithmetic) Pointer * array Pointer to character Pointer to string Pointer" call %y reference+

Introduction , pointer is a special type of varia%le that contains the memory a!!ress of another varia%le+ It points to a varia%le that contains a value+ Declaring pointer variables , pointer varia%le( li-e any normal or!inary varia%le( must %e !eclare! first %efore you can use it in a program+ , pointer shoul! %e !eclare! as %elo " #

Data Datatype type *variable *variable name name

char 5cptr. cptr is a pointer hich points to a varia%le of type character int 5iptr (i. iptr is a pointer hich points to a varia%le of type integer !ou%le 5!ptr( 5!ptr2( !. !ptr(!ptr2 are pointers hich point to varia%les of type !ou%le Pointer operations C uses t o pointer operators( * an! &+ The operator & returns the memory address of its operan! hile the operator * returns the value of the varia%le store! at the a!!ress+ The a!!ress of a varia%le can %e assigne! to a pointer varia%le %y using the a!!ress operator. ampersan! &.

E.g int *m, x=567; m=&x;

____________________________________________________________________ _/!ite! %y" C0Tan12334

CCSB163 Principles of Programming College of Information technology Universiti Tenaga Nasional

The value of a varia%le that the pointer is pointing to can %e accesse! %y using the in!irection or !ereference operator * in front of the pointer name+

E.g int *m, x=567; m=&x; printf ! "n #a$ue of x is =%& "n!, *m';
emember: pointer variables are variables t!at contain addresses o" ot!er variables /&ample 1" 66 pointer operations 7inclu!e 8st!io+h9 main ') : int 5m( &;46<. m;*&. printf '= >n value of & is ;?! >n=( 5m). printf '= >n value of a!!ress & is ;?! >n=( m). printf '= >n value of & is ;?! >n=(&). @

What is the output?

The e&pression *m is use! to access the value store! in the varia%le x+ 'This means that *m references the value store! in the location pointe! %y m)+ $pointer assignment% pointer arit!metic and pointer

Pointer e#pressions comparisons&

Anly a!!ition an! su%traction operations are permitte! in e&pressions involving pointer+ Pointer assignments , pointer varia%le( li-e any normal varia%le( may %e use! on the right han! si!e of an assignment statement to assign its value to another pointer of the same type+ /&ample 2" 66 illustrates the use of pointer 7inclu!e 8st!io+h9 voi! main ' ) :

____________________________________________________________________ _/!ite! %y" C0Tan12334

CCSB163 Principles of Programming College of Information technology Universiti Tenaga Nasional

66!eclare an integer varia%le an! t o pointer int num;4( 5pointer1( 5pointer2. 66assigns the a!!ress of num to pointer1 pointer1 ; *num. 66 assign the value of pointer1 to pointer2 pointer2 ; pointer1. 66!isplay values 4 store! in num t ice since pointer1 an! pointer2 no 66to num+ printf 'B ?! ?! B( 5pointer1( 5pointer2). @ point

What is the output?

/&ample 3" 66assigning values to pointer varia%le

7inclu!e8st!io+h9 voi! main') : int p1(p2(p3. int 5 pt1( 5pt2( 5pt3. p1;13. p2;23. p3;33. pt1;*p1. pt2;*p2. printf'=Calue pointe! %y pt1 is ?!>n=(5pt1). printf'=Calue pointe! %y pt2 is ?!>n=(5pt2). pt3;pt2. printf'= >npt3 is ?!>n=(pt3). printf'=Calue pointe! %y pt3 is ?!>n=(5pt3). 5pt3;D3. printf'=>np2 is ?!>n=(p2). printf'=Calue pointe! %y pt3 is ?!>n=(5pt3). 5pt1;5pt2E13. printf'=>np1 is ?!>n=(p1). printf'=Calue pointe! %y pt1 is ?!>n=(5pt1). pt2 ;*p3.

____________________________________________________________________ _/!ite! %y" C0Tan12334

CCSB163 Principles of Programming College of Information technology Universiti Tenaga Nasional

printf '=>nCalue pointe! %y pt2 is ?! >n=(5pt2). @

What is the output?


Pointer arit!metic Anly t o operations Fa!!ition an! su%traction are permitte! on pointers+ /&ample D" 66 arithmetic operation on pointers 7inclu!e8st!io+h9 main') : int count. int mar-G4H ;:13( 23(33( D3(43@. int 5ptr. printf ' = By using array >n=). for 'count ;3. count 84.countEE) : printf'= ?! >n=(mar-GcountH). @ ptr;*mar-. printf '=By using pointer>n=). for 'count ;3. count 84.countEE) : printf'= ?! >n=(5'ptrEcount) ). @ @

What is the output?

/&ample 4" 66 arithmetic operations on t o pointers

7inclu!e 8st!io+h9 main') : int mar-sGH ; :133( I3(63(D3(23@. int 5ip1( 5ip2( 5ip3. 65 ip1 ta-e a!!ress mar-s G3H 56 ip1 ;*mar-s . printf'= ?! >n=(5ip1).

____________________________________________________________________ _/!ite! %y" C0Tan12334

CCSB163 Principles of Programming College of Information technology Universiti Tenaga Nasional

66ip2 ta-e a!!ress mar-s G1H ip2 ;ip1E1. printf'= ?!>n=(5ip2). 66ip3 ta-e a!!ress mar-s G2H ip3;ip1E2. printf'= ?!>n=(5ip3). 66ip2 ta-e a!!ress mar-sG3H ip2;ip3E1. printf'= ?!>n=(5ip2). 66ip3 ta-e a!!ress mar-sGDH ip3;ip2E1. printf'= ?!>n=(5ip3). 66ip3 ta-e a!!ress mar-s G1H ip3;ip2#3. printf'= ?!>n=(5ip3). @

What is the output?

/&ample 6"

7inclu!e 8st!io+h9 voi! main'voi!) : int num ; 4. int 5numPtr ; *num. printf'=?! >n=( numPtr). printf'=?! >n=( *5numPtr). printf'=?! >n=( 5*numPtr). @

What is the output?

____________________________________________________________________ _/!ite! %y" C0Tan12334

CCSB163 Principles of Programming College of Information technology Universiti Tenaga Nasional

'all (y )alue ** a program that !emonstrate that the parameters passe! to a function using call by value !oes 66 not %eing affecte! %y the function activities+ 7inclu!e 8st!io+h9 voi! mo!ify'int(int). voi! main'voi!) : int a ; 2( % ; D. 66 initialise the varia%le to a value printf'=>nCurrent value" a ; ?! ( % ; ?! =( a( %). 66 calling the function using call %y value mo!ify'a(%). printf'=>n>nThe value of a an! % after the function %eing calle!"=). printf'=>n>na ; ?! ( % ; ?!>n>n=( a(%). printf'=>n>nConclusion" call %y value !oes not change the value of the paremeters=). printf'=>n %eing passe! to a function>n>n=). @ voi! mo!ify'int a( int %) : a ; a E 2. % ; % E 3. printf'=>n>nInsi!e the function( a ; a E 2( % ; % E 3=). printf'=>n>nCurrent value for a an! % insi!e the function are"=). printf'=>na ; ?! ( % ; ?!=(a(%). @ 'all by e"erence ** a program that !emonstrate that the parameters passe! to a function using call by value !oes 66 %eing affecte! %y the function activities+ 7inclu!e 8st!io+h9

____________________________________________________________________ _/!ite! %y" C0Tan12334

CCSB163 Principles of Programming College of Information technology Universiti Tenaga Nasional

voi! mo!ify'int5(int5). 66 using pointer voi! main'voi!) : int a ; 2( % ; D. 66 initialise the varia%le to a value printf'=>nCurrent value" a ; ?! ( % ; ?! =( a( %). 66 calling the function using call %y value mo!ify'*a(*%). 66 instea! of passing the value( e pass the a!!ress of a an! % printf'=>n>nThe value of a an! % after the function %eing calle!"=). printf'=>n>na ; ?! ( % ; ?!>n>n=( a(%). printf'=>n>nConclusion" call %y reference change the value of the paremeters=). printf'=>n %eing passe! to a function>n>n=). @ voi! mo!ify'int 5aptr( int 5%ptr) : 5aptr ; 5aptr E 2. 5%ptr ; 5%ptr E 3. printf'=>n>nInsi!e the function( a ; a E 2( % ; % E 3=). printf'=>n>nCurrent value for a an! % insi!e the function are"=). printf'=>na ; ?! ( % ; ?!=(5aptr(5%ptr). @ +,+ 'I-+-: ./. The follo ing segment of program !isplays the contents of varia%les , an! 0+ ,!! t o more lines of co!e to print the address of the t o varia%les using the a!!ress operator+ int J ; 4( Y ; 13. printf'BThe value of J" ?!>nK( J). printf'BThe value of Y" ?!>nK( Y). .1. ,ssume you have the follo ing !eclarations" int J ; 4( Y ; 13. Intro!uce t o pointer varia%les calle! point, an! point0+ Set point, to the address of , an! point0 to the address of Y+ Linally( !isplay the address of the t o varia%les using the pointer+ .2. The follo ing co!e sums an! pro!ucts the value of & an! y %y using call by value+ Me rite the co!e %y using call by reference+ 7inclu!e 8st!io+h9

____________________________________________________________________ _/!ite! %y" C0Tan12334

CCSB163 Principles of Programming College of Information technology Universiti Tenaga Nasional

int SUN'int(int). int PMA$UCT'int(int). voi! main'voi!) : int & ; 2( y ; 3( sum( pro!uct. printf'=>n>n5## /&ercise 1 ##5>n>n=). printf'=>n>n& ; ?! ( y ; ?!>n>n=(&(y). sum ; SUN'&( y). pro!uct ; PMA$UCT'&( y). printf'=The sum of & an! y" ?!>n=( sum). printf'=The pro!uct of & an! y" ?!>n=( pro!uct). @ int SUN'int &( int y) : int sum. sum ; & E y. return sum. @ int PMA$UCT'int &( int y) : int sum. sum ; & 5 y. return sum. @ .3. The follo ing functions are aime! to s ap the contents of varia%le $A( M/ an! NI 'i+e+ $A ; M/( M/ ; NI( NI ; $A)+ The contents of those varia%les remain the same after s apping+ No!ify the co!e accor!ingly so that the s ap ill ta-e place+ 7inclu!e 8st!io+h9 voi! s ap'int(int(int). voi! main'voi!) : int $A ; 2( M/ ; D( NI ; 6. printf'=>n>n5## /&ercise 2 ##5>n>n=). printf'=Ariginal or!er" ?!( ?!( ?!>n=($A(M/(NI). s ap'$A( M/( NI).

____________________________________________________________________ _/!ite! %y" C0Tan12334

CCSB163 Principles of Programming College of Information technology Universiti Tenaga Nasional

printf'=,fter s apping" ?!( ?!( ?!>n=($A(M/(NI). @ voi! s ap'int $A( int M/( int NI) : int temp. temp ; $A. $A ; M/. M/; NI. NI ; temp. @ Your co!e shoul! %e a%le to pro!uce the follo ing output" Ariginal or!er" 2( D( 6 After Swapping: 4, 6, 2

____________________________________________________________________ _/!ite! %y" C0Tan12334

You might also like