```Arrays and Pointers
Lecture 17
18/2/2002
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
1
Values vs Locations
Variables name memory locations, which hold
values.
1024:
17-Feb-02
32
x
New Type : Pointer
value A pointer contains a reference
name
Sudeshna Sarkar, CSE, IT
Kharagpur
2
Pointer
pointer to int
int x;
int * xp ;
1024:
xp = &x ;
32
x
1024
xp
Pointers Abstractly
int x;
int *p;
p=&x;
...
(x == *p) True
(p == &x) True
*xp = 0;
/* Assign 0 to x */
*xp = *xp + 1; /* Add 1 to x */
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
3
Pointers
A variable in a program is stored in a certain
number of bytes at a particular memory
location, or address, in the machine. A pointer
is the address of a variable in memory.
If v is a variable, &v is its address.
Declare p to be of type pointer to int.
int * p;
p = &i ;
p = NULL;
p = (int *) 1776; /* an absolute
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
4
/*Declaring pointer variables*/
char * cp;
/* pointer to char */
double * dp; /* pointer to double */
int * ip;
/* pointer to int */
Operators for pointers
The indirection operator *
or dereferencing operator
the value pointed to by -Generates the memory address
if ptr = &i, *ptr and i mean
of its operand (must be somethe same thing.
thing that is a memory object)
*ptr = *ptr + 1; /* i=5 */
int i = 4 ;
ptr = ptr+1;
int *ptr ;
ptr = &i;
Sudeshna Sarkar, CSE, IT
stored in ptr. */
17-Feb-02
Kharagpur
5
Pointers
If p is a pointer *p is the value of the
variable of which p is the address.
1. int a=1, b=2, *p;
p
a
b
1
2
2. pa = &a;b
1
2
17-Feb-02
3.
a
1
b = *p ;
b
1
p
p
Sudeshna Sarkar, CSE, IT
Kharagpur
6
Call by value
Program1
void swap1 (int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
}
main
3
i
5
j
swap1
int main (void ) {
int i = 3, j = 5;
35
swap1 (i, j) ;
x
printf (“%d,%d\n”,i,j);
return 0;
3,5 Sudeshna Sarkar, CSE, IT
}
17-Feb-02
Kharagpur
53
y
temp
7
Call by reference: output
parameters
Program2
void swap2 (int *px, int *py) {
int temp;
temp = *px;
*px = *py;
*py = temp;
}
main
1024: 3
i
int main (void ) {
swap2
int i = 3, j = 5;
1024
swap2(&i, &j) ;
px
printf (“%d,%d\n”,i,j);
return 0;
5,3Sudeshna Sarkar, CSE, IT
}
17-Feb-02
Kharagpur
992:
5
j
992
py
8
Dissection of swap2 ()
Call by value is used in C :
whenever variables
are passed as arguments to a function, their values are
copied to the corresponding function parameters, and
the variables themselves are not changed in the calling
environment.
 In program 2, the addresses of the variables i and j (&i
and &j) are passed to swap2. These addresses are
copied to the pointer variables px and py respectively.
px and py thus contain the addresses of i and j (local
variables of main). Using px and py, the locations i and j
can be accessed and their contents can be modified.
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
9
Call by reference
 Some programming languages provide the call-byreference mechanism that can be used to change
the values of variables in the calling environment. C
does not.
 In C, the effect of call-by-reference is achieved by :
1. Declaring a function parameter to be a pointer
2. Using the dereferenced pointer in the function body
3. Passing an address as an argument when the function is
called.
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
10
Vocabulary
Dereferencing or indirection :
following a pointer to a memory location
Output parameter:
a pointer parameter of a function
can be used to provide a value (input),
and/or store a changed value (output)
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
11
scanf demystified
int x, y ;
printf (“%d %d %d”, x, y, x+y) ;
scanf (“%d%d%d”,x,y,x+y);
scanf (“%d%d”, &x, &y);
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
NO !
YES !
12
Why use pointers ?
as output parameters:
functions that need to change their actual
parameters.
to get multiple “return” values:
functions that need to return several results
to create dynamic data structures
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
13
Class Test 1 scripts
Section 5: 5 pm
Room 107, CSE
Section 6 : 5 pm
Room 108, CSE
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
14
Arrays and pointers
An array name is an address, or a pointer
value.
Pointers as well as arrays can be
subscripted.
A pointer variable can take different
An array name is an address, or pointer,
that is fixed.
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
15
Arrays and pointers
int a[20], i, *p;
The expression a[i] is equivalent to *(a+i)
p[i] is equivalent to *(p+i)
When an array is declared the compiler allocates
a sufficient amount of contiguous space in
memory. The base address of the array is the
Suppose the system assigns 300 as the base
address of a. a[0], a[1], ...,a[19] are allocated
300, 304, ..., 376.
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
16
Arrays and pointers
#define N 20
int a[2N], i, *p, sum;
 p = a; is equivalent to p = *a[0];
 p is assigned 300.
 Pointer arithmetic provides an alternative to array
indexing.
 p=a+1; is equivalent to p=&a[1]; (p is assigned 304)
for (p=a; p<&a[N]; ++p)
sum += *p ;
p=a;
for (i=0; i<N; ++i)
sum += p[i] ;
for (i=0; i<N; ++i)
sum += *(a+i) ;Sudeshna Sarkar, CSE, IT
17-Feb-02
Kharagpur
17
Arrays and pointers
int a[N];
a is a constant pointer.
a=p; ++a; a+=2; illegal
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
18
Pointer arithmetic and
element size
double * p, *q ;
The expression p+1 yields the correct machine
address for the next variable of that type.
Other valid pointer expressions:
p+i
++p
p+=i
p-q /* No of array elements between p and q */
17-Feb-02
Sudeshna Sarkar, CSE, IT
Kharagpur
19
```