POINTER:
Pointer is the special variable to find out another variable. OR also we can say the variable that hold memory addresses are called pointers. Thus pointer is a variable that contains an address which is a location of another variable in memory.
To store the address of a variable we have to use the operator “&” called as the address operator.
Ex.:
float *x; // x is a float pointer
int *p; // p is a integer type pointer
int price=420;
p = &price;
printf(“%d”,price); 420
printf(“%u”,&price); 2024
printf(“%u”,p); 2024
printf(“%u”,&p); 2088
printf(“%d”,*p); 420
printf(“%d”,*(&price)); 420
price p
420 2024
2024 2088
Rules for the declaring pointer variable:-
1. The * symbol(asterisk) is used before variable name at the time of declaring. It indicates a pointer variable.
2. A pointer memory allocation it only accepts or stored the memory address of another variable.
3. Which types of memory you want to point out you need to declared that type pointer variable.
Address Operation :
- The address of a variable can be represented by combining ‘&’(Ampersand) with a pointer variable.
- Since the actual location of a variable depends on the system to get the address of an integer variable
- E.g. y=&x;
assigns the address of x to y where y is a pointer. The variable y points to x.
POINTER TO POINTER:
- It is possible to make a pointer to point to another pointer
- It is known as pointer to pointer
- A variable that is a pointer to a pointer must be declared as
int **p;
- This declaration tells the compiler that p is a pointer to a pointer of int type.
- The pointer p is not pointer to an integer, but rather a pointer to an integer pointer.
- E.g.
int x, *p1, *p2;
x=5;
p1=&x;
p2=&p1;
Variable --- x -- P1 -- P2
Contents value 5---1002--5001
Variable Address----1002---5001
-----7002
P2 à p1à x
POINTER WITH ARRAY:
A pointer can be used to point the first element of the array. This address can be then passed to a function. This address can be then used in the function to access all the elements of the array.
Q. Write a program to demonstration of pointers to the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={10,20,30,40,50};
int *p; // pointer variable
p=&a[0]; // assign memory address of a variable
printf("\nAddress of a=%u",p);
printf("\nValue of a=%d",*p);
p=&a[1]; // assign memory address of a variable
printf("\n\nAddress of a=%u",p);
printf("\nValue of a=%d",*p);
p=&a[2]; // assign memory address of a variable
printf("\n\nAddress of a=%u",p);
printf("\nValue of a=%d",*p);
p=&a[3]; // assign memory address of a variable
printf("\n\nAddress of a=%u",p);
printf("\nValue of a=%d",*p);
p=&a[4]; // assign memory address of a variable
printf("\n\nAddress of a=%u",p);
printf("\nValue of a=%d",*p);
getch();
}
POINTER in a STRUCTURE:
A structure is a user defined data type and has many elements. These elements can be accessed using the variable name and dot operator. A pointer can be declared for a structure. The elements of a structure can be accessed using pointer by the (->) arrow operator.
E.g.
struct book
{
int pages;
float price;
} b1, *ptr ;
ptr=&b1;
printf(“%d”,ptr->pages);
printf(“%d”,ptr->price);
0 comments:
Post a Comment