7 Pointers
7 Pointers
Pointers
Slide 2
Recall
If you declare multiple pointers on the same line, you must precede
each of them with an asterisk:
/* one pointer, one regular int */
int *pointer1, nonpointer1;
/* two pointers */
int *pointer1, *pointer2;
Slide 5
Activity
main(){
int a[]={1,2,3,4};
1. Fill in the blanks with
int i;
two statements such
f(a);
that all the elements in
for(i=0;i<4;i++)
the array gets
printf("%d ",a[i]);}
incremented by one.
f(int *p){
int i;
2. Now convert the 2
for(i=0;i<4;i++){
statements into one.
______;
______;
}
}
6
Lab Exercises
Write a function that copies elements of one array into another from a
specific position till the length specified. The function prototype should
look like
void arrayCopy(int *a, int position, int length)
(15 mins)
Write a function that reads a command line argument and prints the
length of the command line arguments excluding the program name.
(15 mins)
7
Slide 8
Array of arrays
int a1[2]={5,6};
int a2[2]={7,8};
a1
int *a[2];//Array of pointers 2000
5 **a
a[0]=a1; 2004
a[1]=a2; 6
a 1000
2008 a2
2000 **(a+1)
a+1 1004 7
2008 2012
8
a[0]*a
a[1]*(a+1)
*a+1
How will you get to 2004?
a[0][1],*(a[0]+1),*(*(a+0)+1) ???
8
Slide 9
int a[2][2]={{5,6},{7,8}};
can be viewed as array of arrays where a[0] is base address of row 1,
a[1] is the base address of row 2 etc.
Base address
a,*a,a[0]
5 **a,a[0][0]
*a+1,a[0]+1 *(*a+1),a[0][1]
6
a+1,*(a+1),a[1] 7 **(a+1),a[1][0]
8
a[0][1]*(a[0]+1)*(*(a+0)+1) 6
a[1][1]*(a[1]+1)*(*(a+1)+1) 8
9
Slide 10
Pointer Operators
10
Slide 11
int main(){
int a[2][2]={{5,10},{15,20}};
printf("%d\n",**a+1);
printf("%d\n",*(*a+1));
printf("%d\n",*(*(a+1)));
return 0;
}
11
Answer :
6
10
55
Slide 12
Array of pointers
2001 10 i
ptrs[0]=&i;
ptrs[1]=&j;
2002
2002 11 j
*(ptrs[1]), ptrs[1][0]11
12
Slide 13
Pointer to a pointer
int i=10
int *j; 2004 1003
int **k; 2004 1003 10
j=&i; k j i
k=&j;
k is a pointer to a pointer to an int
**k10
13
Slide 14
14
Slide 15
Tell me why
The reason behind this is that p contains the address of the string
constant which is stored in the string table.
You can see this by compiling the code with –S option gcc
filename.c –S.
Open filename.s and locate Shankara.
This is a feature of assembly language. “Shankara” is an example of
static data. The string will always be the same, and more importantly,
we know exactly how long it is. Therefore, it may be stored in memory
directly, and does not need to be computed at runtime.
15
Slide 16
Activity
int main(){
char c[4]="abcde";
printf("%s",c);
}
Execute the above code and find out what happens if the size is
specified as
4
5
6
7
Can you change the value of the string variable c as shown below?
c=“pqrst";
16
Answer:
Array of Strings
17
Slide 18
18
Activity
Type and execute the code listed below. Do you see any problem?
main(){
char *names[4];
int i;
for(i=0;i<4;i++){
printf("enter the name \n");
scanf("%s",names[i]);}
for(i=0;i<4;i++){
printf("%s \n",names[i]);
}
}
19
Slide 20
Solution
The problem with the previous program is that the space was not
allocated for the strings.
One way to solve this is by statically allocating space for the strings.
Other way is through dynamic memory allocation (which is coming up)
main(){
char names[6][4];
char *nm[4];
int i;
for(i=0;i<4;i++){
printf("enter the name \n");
20
Slide 21
scanf("%s",names[i]);
nm[i]=names[i];
}
for(i=0;i<4;i++){
printf("%s ",nm[i]);}
}
21
Slide 22
Pointers to functions
22
Slide 23
main(){
int i;
int f();
Prototype declaration
int g();
int (*fptr)(); Function pointer
printf("type 0 for f() or 1 for g() ");
scanf("%d", &i);
if(i)
fptr=g;
Assigning function address to function pointer
else
fptr=f;
(*fptr)(); Calling function through function pointer
return 0;
}
23
Slide 24
int f(){
printf("f() called");
return 0;
}
int g(){
printf("g() called");
return 0;
}
Output:
type 0 for f() or 1 for g() 1 You type this
g() called
24
Slide 25
Activity
What will happen if you omit the parentheses around *x[] in last
statement?
25
Lab Exercise
Create 2 sort functions one that sorts in ascending order and other
that sorts in descending order and other. Based on the user input
call the appropriate sort function at runtime.
(20 mins)
26
Slide 27
27
Let us say we n number of students can enroll for a tutorial and the value of n is unknown to the
programmer because he has no idea t how many students are going to enroll for the tutorial. Maximum
number of enrollments could go upto 100. So, to make program work and to be on safer side,
programmer allocates the memory for 100 students.
And when program is in execution, actual number of students enrolled were 20 only. That means, out of
memory allocated for 100 students only 20 are being used and rest of the 80 memory locations are
wasted as system will not provide it for allocation (as it is already reserved by the programmer)
To solve this issue, a programmer can use dynamic memory allocation to allocate memory for only 20
students at runtime.
Slide 28
If the memory allocation fails (because of unavailability of memory), a null pointer is returned.
If the new size of the memory block is not available in the old location realloc() function allocates a new
memory block in a new location and the content of the memory block is also copied to the new location.
Slide 29
Generic pointer
29
Slide 30
main(){
int *empno, n,i;
printf("enter the number of employees: ");
scanf("%d",&n);
empno=(int *) malloc(n*sizeof(int));
or (int *) calloc(n,sizeof(int));
Check if memory is actually
if(empno=='\0'){ allocated as per the request
printf("memory not available"); or not.
exit(0);}
else
for(i=0;i<n;i++){
printf("\nenter the employee number: ");
scanf("%d",(empno+i));
}
30
Slide 31
31
Slide 32
Heap
32
Slide 33
Lab Exercises
Write a program that takes an input string from command line, creates
a new string by coping an existing string and converting it into upper
case. Display both the input string and the new string with upper case.
Don’t use any library function.
(20 mins)
33
Slide 34
Code segment
Place where the code is stored.
Data segment/ Static memory
Shared address space where the global variables are stored
Remains throughout the program
Stack/ Automatic/Temporary memory
Memory that is automatically created and destroyed
Each function gets its own block of memory
After the function returns stack automatically unwinds
Heap/Free store
Memory where dynamically allocated elements are kept
Grows throughout the life of the of the program as and when required.
34
Slide 35
35
Slide 36
const
36
Slide 37
37
Slide 38
38
Slide 39
int main(){
int k=10;
int l=20;
int *const p=&k;
const int *q=&k;
*p=*p+5; or int const *q=&k;
printf("%d\n",*p);
//*q=*q+5;
change the value of k through pointers
printf("%d\n",*q);
//p=&l; Error!
printf("%d\n",*p);
q=&l; change the address of the pointers
printf("%d\n",*q);
return 0;}
What is this?
const int *const r;
39
Slide 40
Activity
40