Sie sind auf Seite 1von 6

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #20, September 19, 2011

Please switch off your mobile phones.

Announcements

Mid-sem exam copies will be returned in Wednesday Tutorial. Mid sem

Lec-20

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
String fucntions g Array initialization

Lec-20

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Memory Model


Memory is a list of bytes Each byte has an address All variables are stored in memory As many bytes are allocated to the variable as the type of that variable requires Memory for a single variable is contiguous N guarantees whether two consecutively declared No t h th t ti l d l d variables will get contiguous memory areas (bytes) Address of a variable is the address of the first byte where it is stored
Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 3

Recap: Memory Model


i
0 1 2 3 4 5 6 c 7 8 9 10 12

int i; char c; Address of i is 1. It occupies bytes 1 to 4. Address of c is 7. It only occupies byte 7.


Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 4

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

Why we need to know the addresses


If we pass on the addresses in function calls, we can use calls the same memory areas in both callee and called functions.
Like what happened with arrays We did not have to copy large arrays during function calls

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

Have we seen the pointers before


When a variable is read using scanf ( ) Operator & gives location where scanf ( ) should store value Notice the difference between scanf and printf:
We wanted the parameter variable to change after scanf
The changes made within scanf should be visible in the callee function Hence we passed on the address of the variable and not the content or value of the variable l f th i bl

In printf, we did not want the function to make any changes


The value should simply be printed and no changes to be done Hence only the content or value of the variable needed to be passed
Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 7

Have we seen pointers before


Arrays are passed as address Array names are essentially pointers
int a [8];

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

More on pointers and arrays


When a[i] is used, compiler actually computes the address used 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 (t oug (though may be wrong as per the logic of your program) ay w o g pe t e og c o you p og a )

Pointers can be assigned array names:


int *p; p = a;
Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 9

Swapping two integers


void swap (int *pa, int *pb) { int t = *pa; pa pb; *pa = *pb; *pb = t; } int main ( ) { int x = 3, y = 4; int *a, *b; printf (x = %d, y = %d\n , x y); ( x %d %d\n x, a = &x; b = &y; swap (a, b); printf (x = %d, y = %d\n, x, y); }
Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 10

Any Questions?

Lec-20

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

11

Das könnte Ihnen auch gefallen