Upload
sriharsha-p
View
39
Download
0
Embed Size (px)
DESCRIPTION
A keynote on Problem Solving using Computers
Citation preview
CS110 - Intro. To Computing
Pointers !!!!!!!
Lab - 5
• Last Monday’s batch - come today• Last Friday’s batch - class cancelled
because of server problem - cometomorrow - 2 PM
Let us Revise
• What are these?– int *p, *q, *r;
• What are these?– int a,b,c;
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
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
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
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);
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);
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
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.
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;}
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]); }}
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
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
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
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);}
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])}
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;}
int u = 3;int v;int *pu;int *pv;pu = &u;v = *pu;pv = &v;
When you print * and & versions of them? What happens?