Pointer : A Pointer in C language is a variable which holds the address of another variable of same data type.
Sytnax:
datatype *variable_name;
Example:
int *salary; //integer type pointer
char *alphabet; // character type pointer
Concept of Pointers : When a variable is declared in a c program, system allocates a location to hold the assigned value. This location has its own address number. A pointer can also be used to refer another pointer, function.
A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location.
Operators that are used with Pointers
"Address of" (&) Operator
"Value at Address" (*) Operator
Example:
int a=10;
int *p; // declaring integer type pointer ‘p’
p=&a; // assigning address of ‘a’ to pointer ‘p’
printf(‘’%d’’,&p); // print address of ‘p’ that is 3002
printf(“%d”,*p); // print the value of ‘a’ that is 10
Benefits of pointers:
1. Pointers allow passing of arrays and strings to functions more efficiently.
2. Pointers make it possible to return more than one value from the function.
3. Pointers reduce the length and complexity of a program.
4. Pointers increase the processing speed.
5. Pointers save the memory.
Example:
#include<stdio.h>
int main ()
{
int n = 20, *ptr;
ptr = &n; /
printf(“Address of n variable: %d\n”, &n );
printf("Address stored in ptr variable: %d\n", ptr );
printf("Value of *pntr variable: %d\n", *ptr );
return 0;
}
Example:
int i = 12, *ip = &i;
double d = 2.3, *dp = &d;
char ch = 'a', *cp = &ch;
Suppose the address of i, d and ch are 1000, 2000, 3000 respectively, therefore ip, dp and cp are at 1000, 2000, 3000 initially.
Pointer Arithmetic on Integers
|
ip = ip + 1
|
ip = ip + 1 = 1000 + 1*4 = 1004
|
|
ip++ or ++ip
|
ip++ = ip + 1 = 1004 + 1*4 = 1008
|
|
ip = ip + 5
|
ip = ip + 5 = 1008 + 5*4 = 1028
|
|
ip = ip - 2
|
ip = ip - 2 =1028
- 2*4 = 1020
|
|
ip-- or --ip
|
ip = ip + 2 = 1020 + 2*4 = 1028
|
Pointer Arithmetic on Float
|
dp
+ 1
|
dp
= dp + 1 =2000 + 1*8 = 2008
|
|
dp++ or ++dp
|
dp++ = dp+1 = 2008+1*8 = 2016
|
|
dp
= dp + 5
|
dp = dp
+ 5 = 2016+5*8 =2056
|
|
dp
= dp - 2
|
dp = dp
- 2 = 2056-2*8 = 2040
|
|
dp-- or --dp
|
dp = dp
- 1 = 2040-1*8 = 2032
|
Pointer Arithmetic on Characters
|
cp + 1
|
cp = cp + 1 = 3000 +
1*1 = 3001
|
|
cp++ or ++cp
|
cp = cp +
1 = 3001 + 1*1 = 3002
|
|
cp = cp + 5
|
cp = cp +
5 = 3002 + 5*1 = 3007
|
|
cp = cp - 2
|
cp = cp +
5 = 3007 - 2*1 = 3005
|
|
cp-- or --cp
|
cp = cp +
2 = 3005 - 1*1 = 3004
|
Pointer to Pointer
Syntax:
data_type **p;
Example:
int a= 10;
int *p = &a;
int **q = &p;
Here p is of type (int *) or pointer to int, q is of type (int **) or pointer to pointer to int.
**q =*(*q) // both are same
Example:
#include<stdio.h>
int main()
{
int a = 10;
int *p = &a;
int **q = &p;
printf("Value of a = %d\n\n", a);
printf("Address of a = %u\n", &a);
printf("Value of p = %d\n\n", p);
printf("Address of p = %u\n", &p);
printf("Value of q = %d\n\n", q);
printf("Value of *q = value of p = %d\n", *q);
printf("Value of **q = value of a = %d\n\n", **q);
return 0;
}
Pointer to Array
Example:
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int *p = &a[0]
for (i = 0; i < 5; i++)
{
printf("%d", *p);
p++;
}
return 0;
}
Note: *(a+i) is same as a[i]
Pointer to Multidimensional Array
generalized form for using pointer with multidimensional arrays.
*(*(a + i) + j) which is same as, a[i][j]
Pointer and Character strings
Pointer can also be used to create strings. Pointer variables of char type are treated as string.
char *str = "HelloWorld";
Or
char *str;
str = "HelloWorld";
Pointers as Function Argument
Example:
#include <stdio.h>
void swap(int *a, int *b);
int main()
{
int x = 10, y = 20;
printf("x = %d\n", x);
printf("y = %d\n\n", y);
swap(&x, &y); //passing address
printf("After Swapping:\n\n");
printf("x = %d\n", x);
printf("y = %d", y);
return 0;
}
void swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
b = t;
}
No comments:
Post a Comment