2. Lab - 5
• Last Monday’s batch - come today
• Last Friday’s batch - class cancelled
because of server problem - come
tomorrow - 2 PM
3. Let us Revise
• What are these?
– int *p, *q, *r;
• What are these?
– int a,b,c;
4. 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 integer
variables
5. 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
6. 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
7. 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);
8. 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);
9. Let us Revise
• Arrays
– int a[100];
• Now a is a pointer to the start of the
location - address of a variable
• a[5] is a value of the 5th location - name
of a variable
10. Passing Arrays
• All our previous functions we passed
only single values - not arrays.
• In C array is passed “by reference”.
– Implication
• If you change the value inside the function it
gets reflected in the calling function.
11. Example
void 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] = %dn”, count, a[count]);
}
return;
}
12. main() {
int count, a[3];
void modify(int a[]);
printf(“n From main, before calling the
functionn”);
for (count = 0; count <= 2; ++count) {
a[count] = count + 1;
printf(“a[%d] = %dn”,count, a[count]);
}
modify();
printf(“n From main, after calling the
functionn”);
for (count = 0; count <= 2; ++count) {
printf(“a[%d] = %dn”,count, a[count]);
}
}
13. From main, before calling the function:
a[0] = 1
a[1] = 2
a[2] = 3
main() {
int count, a[3];
void modify(int a[]);
printf(“n From main, before calling the
functionn”);
for (count = 0; count <= 2; ++count) {
a[count] = count + 1;
printf(“a[%d] = %dn”,count, a[count]);
}
modify();
printf(“n From main, before calling the
functionn”);
for (count = 0; count <= 2; ++count) {
printf(“a[%d] = %dn”,count, a[count]);
}
}
14. Example
void 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] = %dn”, count, a[count]);
}
return;
}
From the function, after modifying the values:
a[0] = -9
a[1] = -9
a[2] = -9
15. main() {
int count, a[3];
void modify(int a[]);
printf(“n From main, before calling the
functionn”);
for (count = 0; count <= 2; ++count) {
a[count] = count + 1;
printf(“a[%d] = %dn”,count, a[count]);
}
modify(a);
printf(“n From main, after calling the
functionn”);
for (count = 0; count <= 2; ++count) {
printf(“a[%d] = %dn”,count, a[count]);
}
From main, after calling the function:
}
a[0] = -9
a[1] = -9
a[2] = -9
16. Multidimensional Arrays
main() {
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 %dn”,*nrows);
printf(“n Number of columns %dn”,*ncols);
writeoutput(a,*nows,*ncols);
}
17. void readinput(int a[][30], int *m, int *n) {
int i, j;
printf(“Number of rowsn”);
scanf(“%d”,m);
printf(“Number of columnsn”);
scanf(“%d”,n);
for (i = 0; i < *m; i++)
for (j = 0; j < *n; j++)
scanf(“%d”, &a[i][j])
}
18. 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;
}
19. int u = 3;
int v;
int *pu;
int *pv;
pu = &u;
v = *pu;
pv = &v;
When you print * and & versions of them? What happens?