0% found this document useful (0 votes)
21 views15 pages

Pointers in C - Important Concepts

The document discusses pointers in C programming. It defines pointers, shows how to declare and initialize pointer variables, and demonstrates various pointer operations like dereferencing, pointer arithmetic, passing pointers to functions, and using pointers with arrays. Key points include: pointers store the address of a variable; pointers must be initialized before use; valid operations on pointers include incrementing, decrementing, addition/subtraction of an integer; pointers can be used to access elements of an array; pointers allow passing addresses to functions to modify variables.

Uploaded by

Boomslang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views15 pages

Pointers in C - Important Concepts

The document discusses pointers in C programming. It defines pointers, shows how to declare and initialize pointer variables, and demonstrates various pointer operations like dereferencing, pointer arithmetic, passing pointers to functions, and using pointers with arrays. Key points include: pointers store the address of a variable; pointers must be initialized before use; valid operations on pointers include incrementing, decrementing, addition/subtraction of an integer; pointers can be used to access elements of an array; pointers allow passing addresses to functions to modify variables.

Uploaded by

Boomslang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

printf(“\n Address of n = %u”, &n);

The output will be: 2147478276.


printf(“\n Address of n = %x”, &n);
will print FFDC in hexadecimal as the address of variable ‘n’.

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);

int a=6, *ip;


float *p;
char ch=’A’;
ip=&a; ------------- valid
p=&a; ------------- invalid
ip=&ch; ------------- invalid

Any number of pointer variables can point to the same address.


int n=6;
int *x, *y, *z;
x = &n;
y = &n;
z = &n;
Uninitialized Pointers
A pointer should be initialized with another variable’s memory address,
with 0, or with the keyword NULL prior to its use; otherwise the result
may be a compiler error or a run-time error. Consider the following
program.
#include <stdio.h>
int main()
{
int *p; /* a pointer to an integer */
printf(“%d\n”,*p);
return 0;
}
#include <stdio.h>
void main()
{
int x = 10, *p;
p=&x;
printf("Value stored in x: %d\n", x); /* displays the value of x */
printf("Value stored in x :%d\n", *p); /* displays the value of x */
printf("Value stored in x :%d", *(&x)); /* displays the value of x */
}
valid arithmetic operations that can be performed on pointers are shown
below:
1. Incrementing operator (++)
2. Decrementing operator (- -)
3. Addition of integer to a pointer (+)
4. Subtraction of integer from a pointer (-)
5. Subtracting one pointer from another of the same type (-)
6. Assignment of pointers to the same type of pointers
7. Assigning the value 0 to the pointer variable and comparing 0 with
the pointer.
The following arithmetic operations on pointers will not work.
Addition of two pointers
Multiplying a pointer with a number
Dividing a pointer with a number

1 iptr = iptr + iptr + 1 =>


1 1000 + 1*2
=> 1002
2 iptr++ (or) + iptr + 1 =
+iptr 1000 + 1*2
=> 1002
3 iptr = iptr + iptr + 5 =>
5 1000 + 5*2
=> 1010
4 iptr = iptr - 2 iptr - 2 =>
1000 - 2*2
=> 996
5 iptr-- (or) -- iptr - 1 =>
iptr 1000-1*2 =>
998

The following program demonstrates pointer arithmetic between two


pointers:
#include<stdio.h>
int main()
{
int x = 6;
int *p1 = &x;
int y = 12;
int *p2 = &y;
printf("address pointed by p1 = %u\n", p1);
printf("address pointed by p2 = %u\n", p2);
printf("p2 - p1 = %d\n", p2 - p1);
return 0;
}
Output:
address pointed by p1 = 2323001608
address pointed by p2 = 2323001610
p2 - p1 = 1

One-dimensional Arrays and Pointers

int a[]={10, 20, 30, 40, 50};

array a[0] a[1] a[2] a[3] a[4]


a[]
Value 10 20 30 40 50
Addres 259134 259134 259134 259134 259134
s 1840 1842 1844 1846 1848

#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]

In the expression a[i], i must be an integer. For any one-dimensional


array a and integer i, the following relationships are always true.
1. &a[0] == a
2. &a[i] == a + i
3. a[i] == *(a + i)
4. (&a[i] - &a[j]) == (i - j)

A pointer variable p (of the appropriate type) can also be used to


initialize or point to the first element of the array. All the following four
expressions are the same when their addresses are considered.
p[i]
*(p + i)
*(i + p)
i[p]
#include <stdio.h>
int main()
{
int a[]={10, 20, 30, 40, 50};
int i,*p;
p=a; /* it can also be written as p=&a[0]; */
for(i=0;i<5;++i)
printf(“\n%d”, *(p+i)); //it can also be written as p[i] or i[p] or *(i+p)
return 0;
}
#include <stdio.h>
int main()
{
int i;
int a[5] = {10, 20, 30, 40, 50};
int *p =a ;
for (i = 0; i < 5; i++)
{
printf("%d\n", *p);
p++; /* Move the pointer to next element in the array */
}
return 0;
}
The following program illustrate the concept of p[-i]=*(p-i), where p is a
pointer to a one-dimensional array.
#include <stdio.h>
int main()
{
int a[] = {10, 12, 6, 7, 2};
nt i, *p;
p=a+4;
for(i=4; i>=0; i––)
printf(“%d\n”, p[-i]);
return 0;
}

p[–i] is equivalent to *(p–i). Initially p points to the last element. At the


beginning, i=4, (p-i) evaluates as p–i*sizeof(int). Now p-i gives the
address of the first element of the array. *(p–i), prints the first element
of the array. Then i = 3, so p[–i] prints the second element and so on.
If i iterates from 0 to 4, then the code will print the elements of array in
reverse order. The following program illustrates this concept.
#include <stdio.h>
int main()
{
int a[] = {10, 12, 6, 7, 2};
int i, *p;
p=a+4;
for(i=0; i<5; i++)
printf(“%d\n”, p[-i]);
return 0;
}
The pointer to an array need not always point to the first element of the
array. It can point to any element of the array. For example,
int a[]={10,20,30,40,50};
int *p;
p = a + 3;
can also be written as follows,
p = &a[0] + 3; or p = &a[3];
The following program prints the elements from the third position till
the element before the last position of the array.
#include <stdio.h>
int main()
{
int a[] = {10, 12, 6, 7, 2};
int i, *p,*q;
q=&a[4];
for(p=a+2; p!=q; p++)
printf("%d\n", *p);
return 0;
}
Output:
6
7
Subtraction of Pointers
#include <stdio.h>
int main(void)
{
int a[] = {10, 20, 30, 40, 50};
int *p,*q;
p=a;
q=a+4;
printf(“%d”,q – p);
return 0;
}
Output:
4

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

Write a C program to copy an array in reverse order to another


array using pointer variable.
#include<stdio.h>
void main()
{
int a[]={1,2,3,4,5},*p,*q,i,b[5];
p=&a[4];
q=b;
for(i=0;i<5;i++)
{
*(q+i)=*p--;
}
for(i=0;i<5;i++)
printf("%d\t", b[i]);
}
Output:
54321

You might also like