Pointers 050724
Pointers 050724
Pointers
What is a pointer?
• Pointer is a derived data-type in C
• They contain memory addresses as values
• Undoubtedly one of the distinct, powerful and
interesting features of C
Benefits of Pointers
1. More efficient in handling arrays and data tables
2. Can be used to return multiple values from UDFs
3. Permit references to functions, thereby facilitate passing
of functions as arguments to other UDFs
4. Use of pointer arrays to strings results in memory saving
5. Support dynamic memory management
6. Efficient tool for manipulating dynamic DSs such as
structures, Linked lists, Stacks, Queues and trees
7. Reduce length and Complexity of programs
8. Increase execution speed (reduce exec. time)
Definition
RAM int x,y;
Memory Address float a,b;
Cell
char c;
0
1 X a C
2
… 500 600 700
…
int *ptr;
…
ptr Pointer is a variable
500 that can store the
address of another var
800
Declaration
Syntax: data_type *pt_name;
Ex: int *ptr;
int x,*pt1,y;
float * f_ptr;
char* c_pt;
int* a,b,c;
Pointer Initialization
1. int *pt1,a; & - Address of
* - Value at address
ptr1 = &a;
2. float b, *pt2 = &b;
3. char *pt3; Contains garbage
y
10
700
Program
• Write a C program to read an array of integers
and print its elements as it is and then in
reverse order using pointer.
I / P: size of array = 6
a = [4, 0, 1, 15, -6, 2]
O / P: As it is… [4, 0, 1, 15, -6, 2]
Reverse order… [2, -6, 15, 1, 0, 4]
int n,arr[10],i; printf(“Given array\n");
int *ptr1; for(i=0;i<n;i++)
printf(“Enter size of array\n"); {
scanf("%d",&n); printf("%4d",*ptr1);
printf("Enter %d numbers...\n",n);
ptr1++;
for(i=0;i<n;i++)
}
scanf("%d",&arr[i]);
//ptr1 = &arr[0]; printf(“Reverse order ...\n");
ptr1 = arr; for(i=0;i<n;i++)
{
print array elements as it is printf("%4d",*--ptr1);
and then in reverse order }
using pointer.
int a[10],n,i; printf(“Given values are...\n");
printf("Enter size of array\n");
scanf("%d",&n); for(i=0;i<n;i++)
printf(“Enter %d values\n",n); printf(" %d ",*(a+i));
for(i=0;i<n;i++) printf(“Reverse order....\n");
scanf("%d",(a+i)); for(i=n-1;i>=0;i--)
print array elements as printf(" %d ",*(a+i));
it is and then in reverse Enter size of array
order using pointer. 4
Enter 4 values...
3
4
O/P 5
6
Given values are...
3 4 5 6
Given values in reverse order....
6 5 4 3
Program
• Write a C program to read an array of integers
and print only alternate elements as it is and
then in reverse order using pointer.
I / P: size of array = 6
a = [4, 0, 1, 15, -6, 2]
O / P: Alternate elements … [4, 1, -6]
Reverse order… [2, 15, 0]
int a[10],n,i; printf("\nAlternate values...\n");
printf("Enter size of array\n"); for(i=0;i<n;i=i+2)
scanf("%d",&n);
printf(“Enter %d values...\n",n);
printf(" %d ",*(a+i));
for(i=0;i<n;i++) printf(“Reverse order....\n");
scanf("%d",(a+i)); for(i=n-1;i>=0;i=i-2)
printf(“Given values are...\n");
printf(" %d ",*(a+i));
for(i=0;i<n;i++)
Enter size of array
printf(" %d ",*(a+i)); 4
Enter 4 values...
/*alternate values as it is 1
and reverse order*/ 2
3
O/P 4
Given values are...
1 2 3 4
Alternate values...
1 3
Given values in reverse order....
4 2
1) a.) Write a C program to accept the heights of all the boys and girls in a class
and find:
the average height of boys
the average height of girls
the average height of all the students
the height of tallest boy and girl (10)
b.) Write a C program to read the elements of a given matrix of order m×n and find
the sum of odd and even values in it. (05)
2) a. Write the syntax and examples of any five string handling functions.(05)
b. Write a C program to read a sentence and find the number of characters and
words in it without using string handling functions. (05)
c. Write a C program to read a given integer and find its cube using user defined
function with argument and return value.
3) a.) Write a program to accept a given list of total marks (CIE + SEE) of all the
students in a subject and count how many students got “S” grade. (>=90) (05)
b. Define pointer. What are the benefits of using pointers? (05)
c.) Write a C program to accept a list of integers and print them as it is and then in
reverse order using pointers. (05)
int x, y, z,*p, *q; *q = *p + y – 3;
x = 10, y = 15, z = 20; y = y – (*p);
p = &x; *p = *q – z;
q = &z; printf(“%d %d %d”,x,y,z);
……
600 602 604
ptr=ptr+1; ptr
ptr++;
600
++ptr;
Scale factor
• When a pointer is incremented, its value is increased by
‘length’ of the data type that it points to.
Length of various data types:
char : 1 Byte
int : 2 Bytes
long int : 4 Bytes
float : 4 Bytes
double : 8 Bytes
Pointers and Arrays
• When an array is declared, the compiler defines the
array name as a constant pointer to the first element.
int b[4] = {2,6,7,9};
int *p1=b; b
printf(“%d”,*p1); 400
printf(“%d”,*(p1+1));
Relation b/w p and x:
p = &b[0] (= 400) b[0] b[1] . . .
p +1 = &b[1]; (= 402) 2 6 7 9
p +2 = &b[2]; (= 404)
400 402 404 . . .
*(b+i) or *(p+i) represents
element b[i]
Some programs
1. To accept a sorted array of integers and an
integer value, and inserts the value in its
correct place.
ex:
i/p: n = 5, a[5] = {3, 5, 6, 9, 12}, value = 7
o/p: a[5] = {3, 5, 6, 7, 9, 12}
2. Using pointers, write a program that receives
a character string and. a character and deletes
all occurrences of this character in the string.
and print the corrected string with no holes.
Ex:
i/p: string - electronics, character – e
o/p: lctronics
3. Conduct binary search to search for a value in
given list of sorted values using pointers.
ex:
i/p: n = 5, list of sorted values: 9, 6, 4, 1, -4
value to be searched: 5
o/p: found or not
4. Write a function using a pointer parameter
that reverses the elements of a given array.
5. Write a function using pointer parameters
that compares two integer arrays to see
whether they are identical. The function
returns 1 if they are identical, otherwise 0.
Pointer
Arithmetic
Pointer Expressions
Like other variables, pointer variables can be used
in expressions
Ex:
x = *p1 + *p2;
sum = sum + *p2;
If p1 and p2 are pointers to the same array, then:
p1 = p1 + 1; or p1++;
p2 = p1 + 2;
n = p2 – p1
#include<stdio.h>
void main()
{
int a[]={3,4,5,2,1,0}, n,i;
int *p1, *p2;
p1=a;
Output:
p2=&a[5];
n=p2-p1; 3
printf("%d \n",*p1); 05
printf("%d %d",*p2,n);
}