Beruflich Dokumente
Kultur Dokumente
Announcements
Lec-20
Recap
String fucntions g Array initialization
Lec-20
Pointer
A pointer is a variable that stores the address of another variable. A pointer is denoted by the symbol *
int i; int *p; p = &i; // i is a variable of type int // p is a pointer to some integer // p is now address of i (1 in our example)
The address of a variable is denoted by the symbol & p stores the address of i, i.e., &i *p denotes the content pointed by p, i.e., i.
Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 5
When we want the changes made to memory areas in called functions to be reflected in callee function, knowing the addresses of memory areas (variables) are very useful useful. We will also learn about better memory management by asking computer to allocate only as much memory as needed at the run time through the use of pointers.
Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6
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] ( ) q [] 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-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8
Any Questions?
Lec-20
11