19
CS110 - Intro. To Computing Pointers !!!!!!!

Lec21-CS110 Computational Engineering

Embed Size (px)

DESCRIPTION

A keynote on Problem Solving using Computers

Citation preview

Page 1: Lec21-CS110 Computational Engineering

CS110 - Intro. To Computing

Pointers !!!!!!!

Page 2: Lec21-CS110 Computational Engineering

Lab - 5

• Last Monday’s batch - come today• Last Friday’s batch - class cancelled

because of server problem - cometomorrow - 2 PM

Page 3: Lec21-CS110 Computational Engineering

Let us Revise

• What are these?– int *p, *q, *r;

• What are these?– int a,b,c;

Page 4: Lec21-CS110 Computational Engineering

Let us Revise

• What are these?– int *p, *q, *r; - p, q, r are address storing

integer variables• What are these?

– int a,b,c; - a, b, c are names of integervariables

Page 5: Lec21-CS110 Computational Engineering

Let us Revise

• What are these?– int *p, *q, *r;– *p = 5; Content of address p is 5.

• What are these?– int a,b,c;– &a = 0x1000; Address storing variable

named a is 0x1000

Page 6: Lec21-CS110 Computational Engineering

Let us Revise

• Given int *p;– &p is wrong - It means address of address.

• Given int b;– *b is wrong - it means value of value

Page 7: Lec21-CS110 Computational Engineering

Let us Revise

• Function Calls• Given int my_func(int *p);• This can be called as follows:

– int a,b; b = my_func(&a);– int *a; int b; b = my_func(a);– Int *a, *b; *b = my_func(a);

Page 8: Lec21-CS110 Computational Engineering

Let us Revise

• Function Calls• Given int my_func(int p);• This can be called as follows:

– int a,b; b = my_func(a);– int *a; int b; b = my_func(*a);– Int *a, *b; *b = my_func(*a);

Page 9: Lec21-CS110 Computational Engineering

Let us Revise

• Arrays– int a[100];

• Now a is a pointer to the start of thelocation - address of a variable

• a[5] is a value of the 5th location - nameof a variable

Page 10: Lec21-CS110 Computational Engineering

Passing Arrays

• All our previous functions we passedonly single values - not arrays.

• In C array is passed “by reference”.– Implication

• If you change the value inside the function itgets reflected in the calling function.

Page 11: Lec21-CS110 Computational Engineering

Examplevoid modify(int a[]) { int count; printf(“\n From the function, after modifying the values:\n”); for (count = 0; count <= 2; count++) { a[count] = -9; printf(“a[%d] = %d\n”, count, a[count]); } return;}

Page 12: Lec21-CS110 Computational Engineering

main() { int count, a[3]; void modify(int a[]); printf(“\n From main, before calling the

function\n”); for (count = 0; count <= 2; ++count) { a[count] = count + 1; printf(“a[%d] = %d\n”,count, a[count]); } modify(); printf(“\n From main, after calling the

function\n”); for (count = 0; count <= 2; ++count) { printf(“a[%d] = %d\n”,count, a[count]); }}

Page 13: Lec21-CS110 Computational Engineering

main() { int count, a[3]; void modify(int a[]); printf(“\n From main, before calling the

function\n”); for (count = 0; count <= 2; ++count) { a[count] = count + 1; printf(“a[%d] = %d\n”,count, a[count]); } modify(); printf(“\n From main, before calling the

function\n”); for (count = 0; count <= 2; ++count) { printf(“a[%d] = %d\n”,count, a[count]); }}

From main, before calling the function:a[0] = 1a[1] = 2a[2] = 3

Page 14: Lec21-CS110 Computational Engineering

Examplevoid modify(int a[]) { int count; printf(“\n From the function, after modifying the values:\n”); for (count = 0; count <= 2; count++) { a[count] = -9; printf(“a[%d] = %d\n”, count, a[count]); } return;}

From the function, after modifying the values:a[0] = -9a[1] = -9a[2] = -9

Page 15: Lec21-CS110 Computational Engineering

main() { int count, a[3]; void modify(int a[]); printf(“\n From main, before calling the

function\n”); for (count = 0; count <= 2; ++count) { a[count] = count + 1; printf(“a[%d] = %d\n”,count, a[count]); } modify(a); printf(“\n From main, after calling the

function\n”); for (count = 0; count <= 2; ++count) { printf(“a[%d] = %d\n”,count, a[count]); }} From main, after calling the function:

a[0] = -9a[1] = -9a[2] = -9

Page 16: Lec21-CS110 Computational Engineering

Multidimensional Arraysmain() { int *nrows, *ncols; int a[30][30]; void readinput(int a[][30], int *,int *); void writeoutput(int a[][30], int, int); readinput(a,nrows,ncols); printf(“\n Number of rows %d\n”,*nrows); printf(“\n Number of columns %d\n”,*ncols); writeoutput(a,*nows,*ncols);}

Page 17: Lec21-CS110 Computational Engineering

void readinput(int a[][30], int *m, int *n) { int i, j; printf(“Number of rows\n”); scanf(“%d”,m); printf(“Number of columns\n”); scanf(“%d”,n); for (i = 0; i < *m; i++) for (j = 0; j < *n; j++) scanf(“%d”, &a[i][j])}

Page 18: Lec21-CS110 Computational Engineering

void writeoutput(int a[][30], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) printf(“%4d”, a[i][j]); printf(“\n”); }return;}

Page 19: Lec21-CS110 Computational Engineering

int u = 3;int v;int *pu;int *pv;pu = &u;v = *pu;pv = &v;

When you print * and & versions of them? What happens?