Pointers in C
Before knowing about pointers, Let we known about how data are stored in memory so that we can understand pointers more clearly.Let we understand the structure of memory such as:
Above diagram show the structure of memory such as RAM where value or address of any variable is stored. The whole memory is divided into number of partitions and the size of each partition is 1-byte.Memory is start from 0 i.e is lower limit and move upword incremented by 1 to upper limit as show in the diagram.
Now let we understand, if we declare any variable in any program than how compiler will allocate memory. The memory allocation of any variable is depends on type of datatype of variable such as for
- int - 4 byte
- float - 4 byte
- double - 8 byte
- char - 1 byte
Suppose we declare a variable int i; than at the time of execution the compiler allocate 4 byte of memory such as
Suppose we declare another variable char ch; than at the time of execution the compiler allocate 1 byte of memory such as
Let we understand if we initialise any variable? such as i=10; than compiler will place those values in memory in binary form.
So these are the small description about how to manage memory for declaration and initialization of any variable. The question is that is there is any methodology through which we can assess those address or values stored in memory? the answer is Yes by using pointers.
What is Pointers in C ?
We can simply say that pointer contain the address of memory location. We know every thing what ever we had done is stored in memory.Pointer is data type which is used to retrieve data from particular memory location. In this case the address may be the location of variable in memory. If one variable contains the address of another variable the first variable is said to point at the second. We can also say that A pointer is a reference to a memory location.
Let we understand the pointers with the help of diagram.
The above diagram show how we represent a pointer.Here ptr is a pointer which contain the address of int variable i.
It is noted that a pointer can be a NULL which represent pointer point to nothing.
Let we understand some basic type of pointers that we used mostly such as
These pointers are also known as typed pointer which point any specific type of data.
- Integer Pointer. (point to only integer type of data)
- Character Pointer. (point to only character type of data)
- Float Pointer. (point to only float type of data)
- Double Pointer. (point to only double type of data)
- Structure student pointer. (point to only student type of data)
- NULL Pointer.
One more type of pointer is known as Untyped pointer or we can say that generic pointer such as:
- void pointer ( ex: void* a; / void *a; - it can point to any data)
How we declare Pointers in C ?
Syntax:
datatype* variable_name; / datatype *variable_name;
Example:
int* a; / int *a;
We must noted that in pointer operation we need only two operators such as:
- & (address operator)
- * (pointer operator)
We know about address operator it point to the address of any variable and pointer point to the value stored at that address.
Let we understand by an example:
#include <stdio.h>
void main()
{
int i=200;
int * ptr;
ptr = &i;
printf("i=%d\n",i);
printf("ptr=%d\n",ptr);
printf("&i=%d\n",&i);
printf("&ptr=%d\n",&ptr);
printf("*ptr=%d\n",*ptr);
printf("*(&i)=%d\n",*(&i));
}
Out Put:
Practice Questions
- Write a program to display the contents of the pointer.
Program:
Copy Text#include <stdio.h>void main(){int a;int *ptr;a=10;ptr=&a;printf("a=%d ptr=%u\n",a,ptr);printf("a=%d *ptr=%u",a,*ptr);} - Write a program to assign a character variable to the pointer and display the content of pointer.
Program:
Copy Text#include <stdio.h>void main(){char a,b;char *ptr;a='A';ptr=&a;ptr=&a;b=*ptr;printf("value of a=%c\n",a);printf("Pointer value=%c\n",a,b);}
- Write a program to display the address and the contents of a pointer variable.
Program:Copy Text#include <stdio.h>void main(){int a;int *ptr;a=100;ptr=&a;printf("Value of a=%d\n",a);printf("Address of a=%d\n",&a);printf("Value of *ptr=%d\n",*ptr);printf("Value of ptr=%d\n",ptr);}
- Write a program to assign the pointer variable to another pointer and display the contents of both pointer variables.
Program:
Copy Text#include <stdio.h>void main(){int a;int *ptr1,*ptr2;a=100;ptr1=&a;ptr2=ptr1;printf("Value of a=%d\n",a);printf("Value of *ptr1=%d\n",*ptr1);printf("Value of *ptr2=%d\n",*ptr2);}
- Write a program to display the memory address of a variable using pointer before increment/decrement and after increment/decrement.
Program:
Copy Text#include <stdio.h>void main(){int a;int *ptr;a=100;ptr=&a;printf("Memory address before increment=%u\n",ptr);ptr++;printf("Memory address after increment=%u\n",ptr);printf("Memory address before decrement=%u\n",ptr);ptr--;printf("Memory address after decrement=%u\n",ptr);}
- Write a program to add and subtract any integer value with pointer and display value of pointer.
Program:
Copy Text#include <stdio.h>void main(){int a;int *ptr1,*ptr2;a=100;ptr1=&a;ptr2=ptr1+10;ptr1=ptr2-10;printf("Value of a=%d\n",a);printf("Address of ptr1=%u\n",ptr1);printf("Value of ptr1=%u\n",*ptr1);printf("Address of ptr2 after addition=%u\n",ptr2);printf("Value of ptr2 after addition=%d\n",*ptr2);printf("Address of ptr1 after substract=%u\n",ptr2);printf("Value of ptr1 after substract=%d\n",*ptr2);}
Know more about pointers
- Pointers and Functions
- Pointers and Arrays
- Pointers to Pointers
- Command line Arguments
- Dynamic Memory Allocation
No comments:
Post a Comment