Presentation is loading. Please wait.

Presentation is loading. Please wait.

17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.

Similar presentations


Presentation on theme: "17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002."— Presentation transcript:

1 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002

2 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur2 Values vs Locations zVariables name memory locations, which hold values. 32 x 1024: address name value New Type : Pointer A pointer contains a reference to another variable (its address)

3 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur3 Pointer 32 x 1024: int x; int * xp ; 1024 xp xp = &x ; address of x pointer to int *xp = 0;/* Assign 0 to x */ *xp = *xp + 1; /* Add 1 to x */ Pointers Abstractly int x; int *p; p=&x;... (x == *p) True (p == &x) True

4 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur4 Pointers zA 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. zIf v is a variable, &v is its address. zDeclare p to be of type pointer to int. int * p;p = &i ; p = NULL;p = (int *) 1776; /* an absolute address in memory */

5 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur5 /*Declaring pointer variables*/ char * cp; /* pointer to char */ double * dp; /* pointer to double */ int * ip; /* pointer to int */ Operators for pointers The address operator & Generates the memory address of its operand (must be some- thing that is a memory object) int i = 4 ; int *ptr ; ptr = &i; The indirection operator * or dereferencing operator the value pointed to by -- if ptr = &i, *ptr and i mean the same thing. *ptr = *ptr + 1; /* i=5 */ ptr = ptr+1; /* modifies the memory address stored in ptr. */

6 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur6 Pointers zIf p is a pointer *p is the value of the variable of which p is the address. int a=1, b=2, *p; 1 a 2 b p p = &a; 12 abp b = *p ; 11 a b p 1. 3. 2.

7 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur7 Call by value void swap1 (int x, int y) { int temp; temp = x; x = y; y = temp; } int main (void ) { int i = 3, j = 5; swap1 (i, j) ; printf (“%d,%d\n”,i,j); return 0; } 3,5 Program1 main 35 swap1 3 55 3 ij xy temp

8 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur8 Call by reference: output parameters void swap2 (int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; } int main (void ) { int i = 3, j = 5; swap2(&i, &j) ; printf (“%d,%d\n”,i,j); return 0; } 5,3 Program2 main 35 ij swap2 1024992 pxpy 1024: 992:

9 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur9 Dissection of swap2 () zCall 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. zIn 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.

10 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur10 Call by reference zSome programming languages provide the call-by- reference mechanism that can be used to change the values of variables in the calling environment. C does not. zIn 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.

11 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur11 Vocabulary zDereferencing or indirection : yfollowing a pointer to a memory location zOutput parameter: ya pointer parameter of a function ycan be used to provide a value (input), and/or store a changed value (output)

12 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur12 scanf demystified int x, y ; printf (“%d %d %d”, x, y, x+y) ; What about scanf ? scanf (“%d%d%d”,x,y,x+y); NO ! scanf (“%d%d”, &x, &y); YES !

13 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur13 Why use pointers ? zas output parameters: yfunctions that need to change their actual parameters. zto get multiple “return” values: yfunctions that need to return several results zto create dynamic data structures

14 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur14 Class Test 1 scripts zSection 5: 5 pm yRoom 107, CSE zSection 6 : 5 pm yRoom 108, CSE

15 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur15 Arrays and pointers zAn array name is an address, or a pointer value. zPointers as well as arrays can be subscripted. zA pointer variable can take different addresses as values. zAn array name is an address, or pointer, that is fixed.

16 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur16 Arrays and pointers int a[20], i, *p; zThe expression a[i] is equivalent to *(a+i) zp[i] is equivalent to *(p+i) zWhen an array is declared the compiler allocates a sufficient amount of contiguous space in memory. The base address of the array is the address of a[0]. zSuppose the system assigns 300 as the base address of a. a[0], a[1],...,a[19] are allocated 300, 304,..., 376.

17 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur17 Arrays and pointers #define N 20 int a[2N], i, *p, sum; zp = a; is equivalent to p = *a[0]; zp is assigned 300. zPointer arithmetic provides an alternative to array indexing. zp=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) ;

18 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur18 Arrays and pointers int a[N]; za is a constant pointer. za=p; ++a; a+=2; illegal

19 17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur19 Pointer arithmetic and element size double * p, *q ; zThe expression p+1 yields the correct machine address for the next variable of that type. zOther valid pointer expressions: yp+i y++p yp+=i yp-q /* No of array elements between p and q */


Download ppt "17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002."

Similar presentations


Ads by Google