Pointers in C - Important Concepts
Pointers in C - Important Concepts
Initializing Pointers
int n = 6;
int *ptr = &n;
printf(“\nThe address of n using &n is %p”, &n);
printf(“\nThe address of n using ptr is %p”, ptr);
#include <stdio.h>
int main()
{
int a[]={10, 20, 30, 40, 50};
printf(“%u %u %u”, a, &a, &a[0]);
return 0;
}
Output:
2591341840 2591341840 2591341840
#include <stdio.h>
int main()
{
int a[]={10, 20, 30, 40, 50};
int i;
for(i=0;i<5;++i)
printf(“\n%d”, *(a+i)); //it can also be written as a[i] or i[a] or *(i+a)
return 0;
}
All the following four expressions are the same when their addresses are
considered.
a[i]
*(a + i)
*(i + a)
i[a]
Comparing Pointers
#include <stdio.h>
int main(void)
{
int a[] = {10, 20, 30, 40, 50};
int i, *p;
for(p=a; p<=a+4; p++)
printf(“%d\n”, *p);
return 0;
}
Output:
10
20
30
40
50
#include <stdio.h>
int main(void)
{
int array1[5] = {10, 20, 30, 40, 50}, array2[5];
int i, *p1,*p2, *ep;
p2=array2;
ep=&array1[9]; //ep points to the last element of the array
for(p1=array1; p1<=ep; p1++)
*p2++ = *p1;
for(p2=array2; p2<=array2+4; p2++)
printf(“%d\n”, *p2);
return 0;
}
Output:
10
20
30
40
50
Call by Value
#include <stdio.h>
void swap(int,int);
int main()
{
int x=5,y=10;
printf(“Value of x and y before function call”);
printf(“x=%d y=%d\n”,x,y);
swap(x,y);
printf(“Value of x and y after function call”);
printf(“x=%d y=%d\n”,x,y);
return 0;
}
void swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf(“Value of x and y inside function call”);
printf(“x=%d y=%d\n”,x,y);
}
Output:
Value of x and y before function call
x=5 y=10
Value of x and y within function call
x=10 y=5
Value of x and y after function call
x=5 y=10
Call by Address
#include <stdio.h>
void swap(int *,int *);
int main()
{
int x=5,y=10;
printf(“Value of x and y before function call”);
printf(“x=%d y=%d\n”,x,y);
swap(&x, &y);
printf(“Value of x and y after function call”);
printf(“x=%d y=%d\n”,x,y);
return 0;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
printf(“Value of x and y inside function call”);
printf(“x=%d y=%d\n”,*x,*y);
}
#include <stdio.h>
float compute(float, float *);
int main()
{
float r, area, perimeter;
printf(“\n enter the radius of the circle:”);
scanf(“%f”,&r);
area=compute(r, &perimeter);
printf(“\n AREA = %f”, area);
printf(“\n PERIMETER = %f”, perimeter);
return 0;
}
float compute(float r, float *p)
{
float a;
a=(float)3.1415 * r * r;
*p=(float)3.1415 * 2 * r;
return a;
}
#include <stdio.h>
void compute(float *, float *);
int main()
{
float area, perimeter;
compute(&area, &perimeter);
printf(“\n AREA = %f”, area);
printf(“\n PERIMETER = %f”, perimeter);
return 0;
}
void compute(float *a, float *p)
{
float r;
printf(“\n enter the radius of the circle:”);
scanf(“%f”,&r);
*a=(float)3.1415 * r * r;
*p=(float)3.1415 * 2 * r;
}
#include <stdio.h>
void fun(int *, int);
void show(int *, int);
int main()
{
int a[10],i,n,m;
printf(“Enter the size of the array”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
fun(a, n);
show(a, n);
return 0;
}
void fun(int *x, int n)
{
int i;
for(i=0;i<n;i++)
*(x+i)=*(x+i)+10;
}
void show(int *t, int n)
{
int i;
for(i=0;i<n;i++)
printf(“\n%d”,*(t+i));
}
#include <stdio.h>
int main()
{
int i;
int a[5];
for(i = 0; i < 5; i++)
{
*a = 0; // error: lvalue required as increment operand
a++;
}
return 0;
}
#include <stdio.h>
int main()
{
int i;
int *ptr, a[5];
ptr = a;
for(i = 0; i < 5; i++)
{
*ptr = 0; // *ptr accesses a[i]
ptr++;
}
for(i = 0; i < 5; i++)
printf("%d\t",a[i]);
}
return 0;
}
Output:
00000
#include <stdio.h>
int main()
{
int i;
int a[5]={1,2,3,4,5},b[5];
b=a; //error: assignment to expression with array type
return 0;
}
#include <stdio.h>
int main()
{
int i;
int a[5]={1,2,3,4,5},b[5];
for(i = 0; i < 5; i++)
{
b[i]=a[i];
}
for(i = 0; i < 5; i++)
{
printf(“%d\t”,b[i]);
}
return 0;
}
Output:
12345
Write a C program to copy an array to another array using pointer
variable.
#include <stdio.h>
int main()
{
int i;
int a[5]={1,2,3,4,5},b[5],*p1,*p2;
p1=a;
p2=b;
for(i=0;i<5;i++)
{
*(p2+i)=*p1++;
}
for(i = 0; i < 5; i++)
{
printf("%d\t",b[i]);
}
return 0;
}
Output:
12345