c pointer tutorial pdf,pointer in c pdf yashwant kanetkar notes ,pointer in c pdf free download ,pointer in c language pdf,pointer in c programming pdf,pointer in c ppt
Learn and Understand Pointer Basic
Learn and Understand Pointer Basic
Click here Pointer Basic Notes Pdf Download
Definition of pointer: Pointers are variable but different
than normal variable because it store address of another variable.
How to declare Pointer variable and
how it works
You must know two operators
*
Value at
|
&
Address of
|
We can declare pointer variable using * asterisk symbol
Int * ptr, a=5;
Here * ptr is pointer
declaration, a is common integer
variable to understand this concept lets execute simple program
_________________________________
#inclue
#include
void main()
{ int * ptr, a;
clrscr();
printf(“a=%d”, a);
printf(“&a=%d”,& a);
printf(“ptr=%d”, ptr);
printf(“&ptr=%d”,& ptr);
printf(“*ptr=%d”,*ptr);
getch();
}
After execution of this program what is result
Output is like this
a= -20476
&a=-12
Ptr = 12803
&ptr=-14
*ptr=-20476
Let’s see explanation of this program
Output shows garbage
values and cell address of variable
As in our program we
don’t have initialized variable I, e, a and pointer that’s why this result
came.
As shown in figure
In computer memory cell will
allocated as follows
Each cell has address i. e number
value
As shown
a= -20476 &a=-12
ptr = 12803 &ptr=-14
_____________________________
*ptr=-20476?????
As ptr is pointer
variable it will point to something we didn’t yet initialized ptr so by default
it is pointing to some garbage value that’s why this output come.
_________________________________
Lets we initialize pointer and variable
pointer a
#include
#include
void main()
{ int a,*ptr;
printf("
\n \t after initialization");
a=5; ptr=&a;
printf("\n
\t\ta=%d",a);
printf("\n
\t\t&a=%d",&a);
printf("\n
\t\tptr= %d",ptr);
printf("\n
\t\t*ptr=%d",*ptr);
printf("\n
\t\t&ptr= %d",&ptr);
printf("\n \t\t*(*(&ptr))= %d",*(*(&ptr)));
getch();
}
As we use
& to initialize pointer as follows
a=5,
ptr=&a;
Let’s see memory structure
After initialization
In above diagram lets we check the output of some printf
statements
A=5
&a=-12
Ptr=-12
&ptr=-14
*ptr=5
*(&ptr)=-12 eg. (*(-14))=-12
*(*(&ptr)=5
To
print the address of any variable of type pointer which thing we should use
%u treats the integer as unsigned,
whereas
%d treats the integer as signed.
If the integer is between 0 an
INT_MAX (which is 231-1 on 32-bit systems), then the output is identical for
both cases.
It only makes a difference if the integer is negative (for signed inputs) or between INT_MAX+1 and UINT_MAX (e.g. between 231 and 232-1). In that case, if you use the %d specifier, you'll get a negative number, whereas if you use %u, you'll get a large positive number.