Professional Documents
Culture Documents
Pointers Memory allocations What is a pointer Pointer declarations Passing pointers to a functions Relationship between pointers and arrays When to use pointers
Learn
9.1 Variables and memory allocations When variables are declared, the compiler allocated memory locations for these variables. Note that each memory location is identified by an address very much li e a house address. !ust as there are big houses and small houses, some variables occupy more memory spaces and some occupy less.
-ig +.$
"he following variable declarations may result in the memory allocations shown below#
int a,b,c; float x,y char ch;
memory address
a variables b c *
$%%% $%%$ $%%& $%%' $%%( $%%) &%%% &%%$ &%%& &%%' +,
data type int occupied & bytes, so two memory spaces are allocated to each int variable. data type float occupied ( bytes, so ( memory spaces are allocated to each float variable.
Pointers
ch
these values will be stored in memory as shown. a b c * $%%% $%%$ $%%& $%%' $%%( $%%) &%%% &%%$ &%%& &%%' &%%( &%%) &%%. &%%/ '%%%
5 12 67
12.45
45.7
ch
"he memory addresses of the variables can be obtained using the 0 1address of2 operator. "he address of variable a is denoted by &a which is $%%%. -or e*ample, to determine the address of variable a, we can use a variable pa and assign the address to it as in the e*pression#
pa = &a;
++
Pointers
"he statement
pa=&a; /*pa contain th! a""r! of a */
assigns the address of a to variable pa4 pa is now 5pointing6 to a. pa is called a pointer a variable that contains the address of another variable. "he value of a can be accessed by the e*pression *pa, where * is a unary operator, called the indirection operator. 7t operates only on a pointer variables. "he following printf12 statements prints the value of a
printf #$%"&,a'; printf#$%"&,*pa'; /* print /* print th! (al)! of a */ th! (al)! of a too */
9.2 Declaration of pointers 8ll variables have to be declared before they can be used in a function, so are pointer variables. Pointer variables ta e on the data types of the variables that they are used to point to. -or e*ample, pa points to a which is an integer value. 9ence the declaration for pa is
int *pa;
"he program below illustrate the relationship between an integer variable and its associated pointer.
/* *ro+ra, -.1 */ /incl)"!0 t"io.h1 ,ain#' 2 int x= 13; int *px; px=&x; printf #$4h! (al)! of x i x55; #*px'55; 7 %"&,*px';
$%%
Pointers
9.3 Pointers as arg ments in a f nction We have seen that when a function is called by value, the called function does not alter the value of the variable it is only the value of the variable that is supplied to the function and not the variable itself. "o illustrate the concept again, let loo at the 6ap#' function below. "he 6ap#' function swaps the values of two variables.
/* *ro+ra, -.2 8 9 pro+ra, that "o! /incl)"! 0 t"io.h1 ,ain#' 2 int a=<, b=5; (oi" 6ap# a,b'; /* 6ap th! ! t6o (ariabl! */ not "o it :ob; */
printf#$=!for! 6appin+> a=%" an" b=%"?n&,a,b'; 6ap#a, b'; printf#$9ft!r 6appin+> a=%" an" b = %"?n&, a,b'; 7 /* f)nction "!finition */ (oi" 2 6ap#int a, int b' int t!,p; t!,p = a; a=b; b=t!,p;
"here is no swap at all: When the function is called by value, the data item is copied to the function. 8ny alteration done in the function is not carried over to the calling routine. "o solve the 6ap#' function problem, we can pass the addresses of the variables to function. "he address of a variable, once allocated, does not change. We can, then, use pointers to point to these variables and swap their values. 9ence the swap function should be modified as shown.
$%$
Pointers
6ap#pa, pb' int *pa, *pb; 2 int t!,p; t!,p = *pa; *pa = *pb; *pb = t!,p; 7
printf#$=!for! 6appin+> a=%" an" b=%"?n&,a,b'; 6ap#pa, pb'; printf#$9ft!r 6appin+> a=%" an" b = %"?n&,a,b'; 7 /* f)nction "!finition */ (oi" 6ap#int *pa, int *pb' 2 int t!,p; t!,p = *pa; *pa=*pb; *pb=t!,p; 7
"he swap() function is not called by values because the values of variable a and b are not passed to it. What are passed to the function are the addressess of these variables and the swap act on the contents of these addressess. "he function is said to be called by reference.
$%&
Pointers
<our turn $ & ' =nter and run Program +;&. >oes the program swap the variables? =nter and run Program +;'. >oes the program swap the variables? What is the meaning of each of the following declarations ? 1a2 int a,b; 1d2 float a=2.<; 1e2 char c1,c2,c<; 1b2 int *px; float *pa=&a; char *pc1,*pc2,*pc<= &c<; 1c2 float *+; 8 @ program contains the following statements
char ), ( = @ char *p), *p( p(= &(; *p( = (51; ) = *p(51 p)=&);
8ssume that each character occupies $ byte of memory. 7f the value assigned to u is stored at memory location $%%) and the value assigned to v is stored at memory location $%%., then 1a2 What value is represented by &(? 1b2 What value is assigned to p(? 1c2 What value is reprsented by *p(? 1d2 What value is assigned to )? 1e2 What value represented by &)? 1f2 What value is assigned to p)? 1g2 What value is represented by *p)? ) Part of a @ program is shown. =*amine it carefully and answer the Auestions below.
,ain#' 2 char a = A ,b = B; int i,:; int ) !rfnC1#char a, char b'; int ) !rfnC2#char *pa, char *pb'; D
$%'
Pointers
/* f)nction "!finition
b!lo6 */
int ) !rfnC1#char c1, char c2' 2 c1=*; c2=E; D if #c10c2' r!t)rn c1; !l ! r!t)rn c2;
int ) !rfnC2#char *c1, char *c2' 2 *c1=* *c2=E D if #*c1 == *c2' r!t)rn *c1 !l ! r!t)rn *c2 7
Buestions# 1a2 Within main12, what value is assigned to i? 1b2 What value is assigned to C? 1c2 What values are displayed by the first printf12 statement? 1d2 What values are displayed by the second printf12 statement? ** 9.4 Pointers and array Pointers and arrays are closely related. 8 pointers is a variable that ta e 8>>R=33=3 as values and an array name is an 8>>R=33. 3o can you see the relationship ? 8n array name is a pointer which has a fi*ed value 1by the system2 When we declared, for e*ample,
int "ay F7G, *p"ay;
$%(
Pointers
the computer system allocates a base address and a sufficient memory locations for seven elements. 3uppose the system allocates the base address of )%%% for the above array. i.e. address dayD%E is )%%% or 0dayD%EF)%%%, 0dayD$EF)%%&. Recall that the int type data is & bytes long. dayD%E address )%%% dayD$E )%%& dayD&E )%%( dayD'E )%%. dayD(E )%%, dayD)E )%$% dayD.E )%$&
"he statements
p"ay = &"ayF3G
name of array
and
p"ay = "ay
p"ay = 654I4
an"
"ay = 654I4
Loo , they are the same. "hese numbers are computer dependent. <our computer may produce a different set of numbers but the two numbers should be the same. "he value in day cannot be changed because it was assigned by the system. 7t is fi*ed but pday is a pointer variable4 the value in it can be changed. 7t can be used to point to other variables.
$%)
Pointers
"o summarised, an array and the pointer is one and the same thing. 8n array is actually a pointer but the address is fi*ed. When we use a pointer 1e.g. ptest2 to point to a variable, say test. "he assigment statement below is a must.
pt! t = &t! t;
otherwise ptest will not now where to point to. Gut in array declaration, for e*ample
int n),F G;
the address of the base element is automatically assigned to num by the system; a fi*ed address. 9.5 !"en do se pointers# Hse pointers when you want the function to act on variables that are defined in other functions. <ou have to P833 "9= 8>>R=33 I- "9= J8R78GL=3 to the called function. Remember this: 9.6 $trange arit"metic %it" pointers 3uppose, in the array declaration
int t!,pF13G;
the system assigned a base address of )%%% to the $%;element array of data type int and a pointer variable ptmp is declared to point to temp[0], i.e.
pt,p = t!,p;
then will be as shown in the figure below. ptmp )%%% )%%$ )%%& )%%' )%%( )%%) )%%. )%%/ )%%, )%%+ )%$% )%$$ )%$& )%$' )%$(
tempD%E
tempD'E
$%.
Pointers
ptmp has the address value of )%%% and *ptmp points to that address, i.e. tempD%E. Gy definition, ptmp+1 points to the ne*t element 1tempD$E2 and has the address value of )%%&.
i.e.
ptmp+1
F)%%&
When we increment the pointer by $, @ adds $ storage unit 1i.e & bytes for int data type nad ( bytes for float data type2.
/* *ro+ra, -.5. 4o print th! a""r! /incl)"!0 t"io.h1 ,ain#' 2 of (ariabl! */
char chF5G = $4JK4&, *pch; int intCn),F5G = 213,23,<3,43,537, *pint; float fltCn),F5G = 21.1,2.2,<.<,4.4,5.57,*pflt; pch = ch; pint= &intCn),F3G; pflt = fltCn),; printf #$=a ! a""r! printf #$ch = %) /* 9 i+n point!r /* of array 6ith a""r! ! */ */
! of array >&' intCn),=%) fltCn),=%)?n&, ch,intCn),,fltCn),'; printf #$Lal)! of point!r >&'; printf#$pch=%) pint=%) pflt=%)?n&,pch,pint,pflt'; printf#$?nincr!a ! point!r by 1 an" th! a""r! ar!>?n&'; pch55; pint55; pflt55; printf #$Lal)! of point!r >&'; printf#$pch=%) pint=%) pflt=%)?n&,pch,pint,pflt';
9ft!r incr!a in+ point!r by 1 an" th! a""r! ! Lal)! of point!r >pch=6545- pint= 65466
"he above results shows that when a char pointer is increased by $, its value is increased by $ 1$ bytes2, when an int pointer is increased by $, its value is increased by & 1for & bytes2 and when a float pointer is increased by $, its value is increased by ( 1( bytes2. Note that these numbers are machine dependent4 when you run this program you may get a different set of values.
$%/
Pointers
<our turn $ =nter and run Program +;) and fill in the address columns of the arrays in the table below# ch array intKnum array fltKnum array address address address chD%E intKnumD% fltKnumD%E E chD$E intKnumD$ fltKnumD$E E chD&E intKnumD& fltKnumD&E E chD'E intKnumD' fltKnumD'E E chD(E intKnumD( fltKnumD(E E 1a2 What is the value of pch L'? 1b2 What is the value of pintL(? 1c2 What is the value of pfltL&? 1d2 What is the value of M1pchL&2? 1e2 What is the value of M1pintL$2? 1f2 What is the value of M1pfltL&2? & ' ( ) 9ow many bytes in the memory are allocated for data type int? 9ow many bytes in the memory are allocated for data type char? 9ow many bytes in the memory are allocated for data type float? =*amine the portion of the @ program below and anwer the following Auestion.
,ain#' 2
$%,
Pointers
/* f)nction "!finition */ (oi" ) !rfn #int *p'; 2 int i, ),=3; for #i=3 ; i0=5; i55' 2 ), 5=*#p5i'; 7 printf #$ ), = %"?n&, ),'; r!t)rn; 7
1c2 What is the purpose of the for loop that appears within the userfn12?
**
$%+