esc101-lec21
TRANSCRIPT
-
7/31/2019 esc101-lec21
1/9
1
ESc101: Fundamentals of Com utin
2011-12-Monsoon Semester
Lecture #21, September 20, 2011
Please switch off your mobile phones.
Announcements
Mid-sem exam co ies will be returned in Wednesda Tutorial.
Lec-21 1Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
-
7/31/2019 esc101-lec21
2/9
2
Recap
Pointers
Variable declaration
Memory model and addresses
* and & operators
Arrays as pointers
Lec-21 2Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
Recap: Pointer
variable.
A pointer is denoted by the symbol *
int i; // i is a variable of type int
int *p; // p is a pointer to some integer
The address of a variable is denoted by the symbol &
p = &i; // p is now address of i (1 in our example)
p stores the address of i, i.e., &i
*p denotes the content pointed by p, i.e., i.
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
3
-
7/31/2019 esc101-lec21
3/9
3
Recap: Have we seen pointers before
Arrays are passed as address
Array names are essentially pointers
a is a pointer to the first element of the array, i.e., a[0]
*a is equivalent to a[0]
Since array elements are stored contiguously in memory
(a + i) is a pointer to a[i]
*(a + i) is equivalent to a[i]
Notice that pointer arithmetic is different from normal
arithmetic
When we add 1 to a, we are actually telling the computer to get
the address of the next integer (or whatever type array is of)
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
4
Recap: More on pointers and arrays
,
of (a + i) and accesses the variable in that address
If the computer has 4-byte integers, then (a+i) would
actually translate to a + 4i, where a is the address of a[0] No error checking on array boundaries is done
Therefore, a[-2], a [12], etc., become legal in C language
thou h ma be wron as er the lo ic of our ro ram
Pointers can be assigned array names:
int *p;
p = a;
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
5
-
7/31/2019 esc101-lec21
4/9
4
Recap: Swapping two integers
void swap (int *pa, int *pb)
{
int t = *pa;
* = *
*pb = t;
}
int main ( )
{
int x = 3, y = 4;
int *a, *b;
= = , , ,
a = &x; b = &y;
swap (a, b);
printf (x = %d, y = %d\n, x, y);
}
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
6
Pointer Operations
Integers can be added to or subtracted from a pointer
Adds (or subtracts) in units of size of type pointed to
int *p, *q, i;
p = &i; // suppose p is 2001
p++; // p is now 2005 (assuming int is 4 bytes)
q = p -2; // q is now 1997 Pointer cannot be multiplied or divided by an integer
Pointers can be compared (useful in arrays)
It signifies that pointer points to nothing
It is equivalent to 0
No variable can be allotted memory starting address 0
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
7
-
7/31/2019 esc101-lec21
5/9
5
Pointer Operations
#include
int main ( )
{
n , , p, q;
// Assigning constant to a pointer
p = 12345678; // Warning, may result in segmentation fault
printf (%d\n, *p); // when you de-reference this pointer
// Assigning NULL to a pointer
p = ;printf (%d\n, *p); // Will certainly result in segmentation fault
// Size of pointer on your machine
printf (%d\n, sizeof (p)); // Will print 8 on lab machines
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
8
Pointer Operations
// Printing a pointer
p = &i;
printf (%u\n, p);
ncrement ng, ecrement ng a po nter
q = p 1;
p++;
printf (%u\t%u\n, p, q);
// Comparing two pointers
printf (%d\n, (p < q));
// Storing values in memory areas pointed to by pointer variables
p = &i; q = &j;
*p = 7; *q = 8;
printf (%d\t%d\t%d\t%d\t\n, (*p), (*q), i, j);
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
9
-
7/31/2019 esc101-lec21
6/9
6
Pointer Operations
// Assigning a pointer to another pointer
p = q;
printf (%d\t%d\n, (*p), (*q));
*p = 4;
printf (%d\n, (*q));
// Subtracting a pointer from another pointer
= + 10;
printf (%d\n, (q p));
}
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
10
Passing arrays as pointers
#include
void read_array (int *p, int size)
{
{
printf (%d\t, *p);
p++; i++;
}
int i = 0;
while (i < size)
{
scanf (%d, p);
p++; i++;
}
}
printf (\n);
}
int main ( )
{
int a [ ] = {3, -2, 7, 19};
print_array (a, 4);
read arra (a, 4 ;
void print_array (int *p, int size)
{
int i = 0;
while (i < size)
_
print_array (a, 4);
}
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
11
-
7/31/2019 esc101-lec21
7/9
7
Passing pointers as arrays
The pointer must point to the correct array address
Pointer may point to the middle of the array, and
operations may start from there
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
12
Passing arrays as pointers
#include
void read_pointer (int a[ ], int size)
{
{
printf (%d\t, a[i]);
i++;
}
int i = 0;
while (i < size)
{
scanf (%d, &a[i]);
i++;
}
}
printf (\n);
}
int main ( )
{
int b [ ] = {3, -2, 7, 19};
int *p = b;
rint ointer ( , 4 ;
void print_pointer (int a[ ], int size)
{
int i = 0;
while (i < size)
_
print_pointer (p+1, 3);
read_pointer (p, 4);
print_pointer (p, 4);
}
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
13
-
7/31/2019 esc101-lec21
8/9
8
Sizes of variables
int *p; a[10];
printf (%d\n, sizeof (int));
printf (%d\n, sizeof (char));
printf (%d\n, sizeof (float));
printf (%d\n, sizeof (double));
pr nt n , s zeo ong nt ;printf (%d\n, sizeof (p));
printf (%d\n, sizeof (a));
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
14
String copy using arrays
void strcpy (char s [ ], char t [ ])
{
int i = 0;
while (t[i] != \0)
{
s[i] = t[i]; // copy
i++; // point to next element
}
s[i] = \0; // terminate s
}
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
15
-
7/31/2019 esc101-lec21
9/9
9
String copy using pointers
void strcpy (char *s, char *t)
{
while (*t != \0)
{
*s = *t; // copy
s++; // point to next element
t++; // point to next element
}
*s = \0; // terminate s
}
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
16
Any Questions?
Lec-21 17Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon