Pointers in C
1
INTRODUCTION
A pointer is a derived data type in c.
Pointers contains memory addresses as their values.
A pointer is a variable whose value is the address of another variable,
i.e., direct address of the memory location.
Like any variable or constant, you must declare a pointer before
using it to store any variable address.
Pointers can be used to access and manipulate data stored in the
memory.
2
Advantages
Pointers make the programs simple and reduce their length.
Pointers are helpful in allocation and de-allocation of memory
during the execution of the program. Thus, pointers are the
instruments dynamic memory management.
Pointers enhance the execution speed of a program.
Pointers are helpful in traversing through arrays and character
strings.
Passing on arrays by pointers saves lot of memory because we are
passing on only the address of array instead of all the elements of
an array.
Pointers are used to construct different data structures such as
linked lists, queues, stacks, etc.
3
ACCESSING THE ADDRESS
VARIABLE
The operator & immediately preceding a variable #include <conio.h>
returns the address of the variable associated with
void main()
it.
Example
{
p=&quantity; int x=125;
• Would assign 5000 (the location of float p=20.345;
quantity) to the variable p. char a=‘a’;
The & operator is a address operator. clrscr();
The & operator can be used only with a simple printf(“%d is stored at addr %u\n”,x,&x);
variable or an array element.
printf(“%f is stored at addr %u\n”,p,&p);
printf(“%c is stored at addr %u\n”,a,&a);
getch();
}
#include <stdio.h>
4
DECLARING POINTER
VARIABLES
Syntax
data_type *pt_name;
• The * tells that the variable pt_name is a name of the pointer
variable.
• Pt_name needs a memory location.
• Pt_name points to a variable of type data_type.
Example
int *p;
• Declares the variable p as a pointer variable that points to an
integer data type.
• The declarations cause the compiler to allocate memory locations
for the pointer variable p.
5
INITIALIZATION OF POINTER
VARIABLES
The process of assigning the address of a variable to a pointer variable is known as
initialization.
All uninitialized pointers will have some unknown values that will be interpreted as
memory addresses.
They may not be valid addresses or they may point to some values that are wrong.
• Once a pointer variable has been declared we can use the assignment operator to initialize
the variable.
Example
1. int q; int *p; p=&q;
2. int q; int *p=&q;
3. int x,*p=&x
Illegal statement int *p=&x, x;
• We can also define a pointer variable with an initial value to NULL or 0.
int *p=null:
int *p=0;
6
POINTER FLEXIBILITY
Pointers are flexible.
We can make the same pointer to point to different data variables in different statements.
Example
int x, y, z, *p
……………
*p=&x;
……………
*p=&y;
……………
*p=&z;
……………
We can also use different pointers to point to the same data variable.
Example
int x;
int *p1=&x;
int *p2=&x;
int *p3=&x;
With the exception of NULL and 0, no other c onstant value can be assigned to a pointer variable.
7
ACCESSING A VARIABLE THROUGH
ITS POINTERS
We can access the value of another #include <stdio.h>
variable using the pointer variable. int main(void)
Steps: {
• Declare a normal variable, assign the //normal variable
value. int num = 100;
• Declare a pointer variable with the //pointer variable
same type as the normal variable. int *ptr;
• Initialize the pointer variable with the //pointer initialization
address of normal variable. ptr = #
• Access the value of the variable by //pritning the value
using asterisk (*) - it is known as printf("value of num = %d\n", *ptr);
dereference operator (indirection return 0;
operators). }
8
#include <stdio.h>
EXAMPLE
void main() Output
{ Value of x is 10
int x,y;
int *ptr;
10 is stored at address 2996846848
x=10;
ptr=&x; 10 is stored at address 2996846848
y=*ptr; 10 is stored at address 2996846848
printf("Value of x is %d\n",x); 298120448 is stored at address 2996846856
printf("%d is stored at address %u\n",x,&x); 10 is stored at address 2996846852
printf("%d is stored at address %u\n",*&x, &x); New value of x =100
printf("%d is stored at address %u\n",*ptr,ptr);
printf("%d is stored at address %u\n",ptr,&ptr);
printf("%d is stored at address %u\n",y,&y);
*ptr=100;
printf("\nNew value of x =%d\n",x);
}
9
POINTER EXPRESSIONS
Pointer variables can be used in expressions In addition to arithmetic operations ,the
Example pointer can also be compared using the
• If p1 and p2 are properly declared and relational operators.
• p1>p2
initialized pointers then the following • p1==p2
statements are valid. • p1 != p2
y= *p1 * *p2; y=(*p1) * (*p2) We may not use pointers in division or
sum= sum + *p1; multiplications.
z=5* - *p1/ *p2 (5* (-(*p1)))/(*p2); p1/p2
• There is blank space between / and p1 * p2
*p2 p1/3
*p2= *p2 + 10;
p1+4;
p2-2;
p1-p2;
p1++;
-p2;
sum += *p2;
10
Example
1. #include <stdio.h> 17. z= *p1 * *p2 -7;
2. int main() 18. printf("a= %d\tb=%d\n",a,b);
3. { 19. printf("*p1 = %d\n",*p1);
4. int a, b,*p1, *p2,x,y,z; 20. printf("*p2 = %d\n",*p2);
5. a=10; 21. printf("z= %d\n",z);
6. b= 5; 22. return 0;
7. p1=&a; 23. }
8. p2=&b; 24. Output
9. x= *p1 * *p2; 25. Address of a = 71870892
10. y= *p1 + *p2; 26. Address of b = 71870896
11. printf("Address of a = %u\n",a); 27. a= 10 b=5
12. printf("Address of b = %u\n",b); 28. x= 50 y=15
13. printf("a= %d\tb=%d\n",a,b); 29. a= 5 b=10
14. printf("x= %d\ty=%d\n",x,y); 30. *p1 = 5
15. *p2= *p2 +5; 31. *p2 = 10
16. *p1= *p1-5; 32. z= 43
11
POINTER INCREMENT &
SCALE FACTOR
p1++;
The pointer p1 to point to the next value of its type.
If p1 is an integer pointer with an initial value, say 4020, then the operation p1++, the
value of p1 will be 4022.
i.e, the value increased by the length of the data type that it points to.
char 1 byte
int 2 bytes
float 4 bytes
long int 4 bytes
double 8 bytes
12
POINTERS AND ARRAYS
The address of &x[0] and x is the same. It’s because the variable name x points to
the first
element of the array.
•&x[0] is equivalent to x. And, x[0] is equivalent to *x.
Similarly, &x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
&x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).
Basically, &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).
13
WAP to read 5 integers and store them in an array using pointers.
Print their sum and mean
1. #include <stdio.h> 19. return 0;
2. int main() 20. }
3. { 21. Output
4. int i, x[20], sum = 0,n; 22. Enter the value of n: 5
5. float avg=0; 23. Enter number one by one
6. printf("Enter the value of n: "); 24. 5
7. scanf("%d",&n); 25. 10
8. printf("Enter number one by one\n"); 26. 15
9. for(i = 0; i < n; ++i) 27. 20
10. { 28. 25
11. /* Equivalent to scanf("%d", &x[i]); */ 29. Sum is = 75
12. scanf("%d", x+i); 30. Mean is = 5
13. // Equivalent to sum += x[i]
14. sum += *(x+i);
15. }
16. printf("Sum is = %d", sum);
17. avg=(float)sum/5;
18. Printf(“\nMean is =%f”,avg);
14