What is pointer in c programming language?
![]() |
Pointer in c. |
The main purpose of a pointer is to get the memory address of variable which is defined in the program code.
Pointer is special variable. it holds the memory address of the variable of the same data type. Without the help of a pointer, you cannot perform tasks such as dynamic memory allocation and many tasks in the c programming language.
The actual data type of the value of all pointers, whether it's an integer, a float, a character, or other, has the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.
Assigning addresses to pointers :
Example :
int a=6;
int *ptr;
ptr=&a;
Pointer declaration :
Data_type *pointer_Name;
Declaration | Type |
---|---|
int *p | Pointer to an integer |
double *p | Pointer to a double |
float *p | Pointer to a float |
char *p | Pointer to a char |
How to use pointers:
These are a few important operations which we will do with the help of pointer frequently :
- We define a pointer.
- Assign the address of a variable.
- Finally access the value at the address available in the pointe.
Example :
#include<stdio.h>
int main()
{
int a = 30;
int *ptr;
ptr = &a;
printf("address of variable:%x\n",&a);
printf("address stored in p variable:%x\n",ptr);
printf("value of *ptr variable:%d\n",ptr);
return 0;
}
What is Null pointer in c programming language?
It is always a good practice to assign a null value to a pointer variable, in case you do not have an exact address to be assigned. This is done at the time of variable declaration a pointer that is assigned null value is called a null pointer.
The null pointer is a constant with a value of zero defined in several standard libraries.
Example :
#include<stdio.h>
int main()
{
int *ptr = null;
printf("the value of ptr is :%x\n",ptr);
return 0;
}
Pointer arithmetic :
Pointer arithmetic may be defined as a process of declaration of pointer arithmetic calculation to represent the variable in c programming language.
Pointer variable are also know as address data types because they are used to stored the address of another variable.
the address is the memory location that is assigned to the variable. It doesn’t store any value.
Some arithmetic pointer operations are :
- Increment /Decrement of a pointer.
- Addition of integer to a pointer.
- Subtraction of integer to a pointer.
- Subtracting two pointers of the same type.
Increment :
It is a condition that also comes under addition. When a pointer is increment. It actually increment by the number equal to the size of the data type for which it is a pointer.
Decrement :
It is a condition that also comes under subtraction. When a pointer is decrement. It actually decrement by the number equal to size of the data type for which it a pointer.
Example :
#include<stdio.h>
int main()
{
int N = 4;
int *ptr1, *ptr2;
ptr1 = &N;
ptr2 = &N;
printf("Pointer ptr1 before Increment: ");
printf("%p \n", ptr1);
// Incrementing pointer ptr1;
ptr1++;
printf("Pointer ptr1 after Increment: ");
printf("%p \n\n", ptr1);
printf("Pointer ptr1 before Decrement: ");
printf("%p \n", ptr1);
// Decrementing pointer ptr1;
ptr1--;
printf("Pointer ptr1 after Decrement: ");
printf("%p \n\n", ptr1);
return 0;
}
Addition of pointer :
When a pointer is added with a value, the value is first multiplied by the size of data type and then added to the pointer.
Example :
#include<stdio.h>
int main()
{
int a = 5;
int *ptr1;
int *ptr2;
ptr1 = &a;
ptr2 = &a;
printf("pointer ptr1 before increment\n");
printf("%x\n", ptr1);
ptr1++;
printf("%x\n", ptr1);
printf("pointer ptr2 before decrement\n");
printf("%x\n", ptr2);
ptr2--;
printf("%x\n", ptr2);
return 0;
}
Subtraction of Pointer :
When a pointer is subtracted with a value, the value is first multiplied by the size of data type and then subtracted to the pointer.
Like pointer addition, we can subtract a value from the pointer variable. Subtracting any number from a pointer will give an address.
Example :
#include<stdio.h>
int main()
{
int a = 10;
int *ptr1;
int *ptr2;
ptr1 = &a;
ptr2 = &a;
printf("pointer ptr1 before increment\n");
ptr1 = ptr1 - 3; // subtraction of pointer
printf("%d\n", ptr1);
printf("pointer ptr2 before decrement\n");
ptr2 = ptr2 - 6; // subtraction of pointer
printf("%d\n", ptr2);
return 0;
}
Subtracting two pointers of the same type :
The subtraction of two pointer is possible only when they have the same data type.
The result is generated by calculation the difference between the addresses of the two pointer and calculating how many bits of data it is according to the pointer data type.
The subtraction of two pointers gives the increment between the two pointers.
Example :
#include<stdio.h>
int main()
{
int a;
int n = 5;
int *ptr1, *ptr2;
ptr1 = &n;
ptr2 = &n;
ptr2 = ptr2 + 4; // incrementing ptr2 value
a = ptr2 - ptr1;
printf("subtraction of ptr1 and ptr2 is :%p\n", a);
return 0;
}
Pointer arithmetic on array :
Pointer arithmetic on array contain address, adding two address makes no sense because these is an idea what it would point to. Subtracting two address lets you compute the offset between the two addresses.
An array name acts like a pointer constant. The value of this pointer constant is the address of the first element.
Example :
#include<stdio.h>
int main()
{
int N = 5;
int arr[] = { 1, 2, 3, 4, 5 };
int* ptr;
ptr = arr;
for (int i = 0; i < N; i++) {
printf("%d ", ptr[0]);
ptr++;
}
return 0;
}
Function pointer in c programming language :
Pointer gives great possibilities to c function which we are limited to return one value. With pointer parameters, our function now can process actual data rather than a copy of data.
Function pointer point to code. In function pointers, function’s name can be used to get function’s address.
A function can also be passed as an argument and can be returned from a function.
Example :
#include<stdio.h>
int add(int, int);
int main()
{
int x, y;
int (*ptr)(int, int);
int result;
printf("enter the number of xand y:");
scanf("%d%d", &x, &y);
ptr = add;
result = (*ptr)(x, y);
printf("value after addition is :%d", result);
return 0;
}
int add(int x, int y)
{
int z = x + y;
return z;
}
0 Comments
Post a Comment