A pointer is a variable
that stores the address of another variable.
Uses:
It is used to allocate
memory dynamically
It is used in structure, functions,
and arrays
Notes
& (ampersand sign)
is called address of operator
and it is used to determine the address of a variable
* (asterisk sign)
is called indirection operator
and it is used to access the value at the address
Declaration of pointer
Datatype *pointername
Example:
int *x; // pointer to int
Float *y; // pointer to float
Char *ch // pointer to char
Note:
%d : it stores value of a variable
%x or %p: it stores address of a pointer
Give An Example
Of Pointer
#include<stdio.h>
#include<conio.h>
void main(){
int x=50;
clrscr();
int
*p;
p=&x;//stores
the address of number variable
printf("Value of variable var is: %d\n", x);
printf("Value of variable var is: %d\n", *p);
printf("Address of variable var is: %x\n", &x);
printf("Address of variable var is: %x\n", p);
printf("Address of pointer p is: %x", &p);
getch();
}
Output:
Value of variable var is: 50
Value of variable var is: 50
Address of variable var is: cfb7d734
Address of variable var is: cfb7d734
Address of pointer p is: cfb7d738
Explanation
Pictorial representation
int x=50;
int *p
P=&x
cfb7d738 (address) cfb7d734 (address)
cfcfb7d734
Cfb7d734
|
50
|
p (pointer)
x(variable)
Explanation:
x=50;
*p = x=50
&x= cfb7d734;
p=&x= cfb7d734
&p= cfb7d738
No comments:
Post a Comment