Pointer is a variable that store address of another variable but same type so that we can access data of first (normal) variable.
Situation: suppose a postman delivers letters in a city. He know where is address. Someday he is on leave. So What happen with delivery of letters. Post office appoint new postman temporary and he delivered letters for that day.
Conclusion: We must have second way to reach that address .
This mechanism is also applied with programming language. Where we have second way to access variables using pointer.
Q. question is how this is done ?
A. Simple as pointer definition it is used to store address of another variable. This phenomena we use to make second way to reach that address.
Task first how pointer declared ?
datatype * variable name;
this declare a pointer variable.
Task second how to assign address to pointer of another variable ?
pointer variable= & normal variable;
using address of operator we assign address of normal variable to pointer.
Task third how to access value of normal variable using pointer ?
using *pointer variable;
above pattern tells the pointer that go it's stored address and access normal variable value.
example
int i=10;
int *ptr;
ptr=&i;
access value using *ptr;




No comments:
Post a Comment