Beruflich Dokumente
Kultur Dokumente
Arithmetic operations on pointer variables are also possible. Increase, decrease, prefix and postfix
operations can be performed with the help of the pointers.
Program
#include<stdio.h>
main()
p=&a;
j=&b;
Output
Addition a+b=35
Subtraction a-b=15
Product a*b=250
Division a/b=2
Modulo=5
Program
#include<stdio.h>
main()
int i;
int a[5]={1,2,3,4,5};
int *p=a;
for(i=0;i<5;i++)
printf("%d\t",*p);
p++;
Output
1 2 3 4 5
Another example program
#include<stdio.h>
main()
int i;
int a[5]={1,2,3,4,5};
for(i=0;i<5;i++)
printf("%d\t",*(a+i));
Output
1 2 3 4 5
i) a[i]
ii) *(a+i)
iii) *(i+a)
iv) i[a]
Array of Pointers
Syntax
data_type *array_name[size];
Example
int *arrp[3];
Program
#include<stdio.h>
main()
int *arrp[3];
arrp[0]=&a;
arrp[1]=&b;
arrp[2]=&c;
for(i=0;i<3;i++)
Creating a string
In the following example, we are creating a string str using the char character array of size 6
char str[6]=”Hello”;
0 1 2 3 4 5
H e l l o \0
#include<stdio.h>
main()
gets(name);
while(*ch!=’\0’)
printf(“%c”, *ch);
ch++;
Output
Siva
Pointer as a function parameter is used to hold addresses of arguments passed during function
call.
This is known as call by reference.
When a function is called by reference, any change made to the reference variable will affect
the original variable.
Program
#include<stdio.h>
printf(“m=%d\n”, m);
printf(“n=%d\n”, n);
swap(&m, &n);
printf(“After swapping:\n”);
printf(“m=%d\n”, m);
printf(“”n=%d\n”, n);
return 0;
int temp;
temp=*a;
*a=*b;
*b=temp;