What are Pointers?
Did you ask anyone what is the most difficult
thing to learn in C. I can bet, 95% of the learners will say, "Huh, it is
Pointers". Agreed ?
The only thing why we feel it because C uses the pointers extensively, but
believe me, the concept of the pointers is very simple. The only problem is,
its terminology, e.g. use of & and * and we get confused where to use what?
Am I Right?
So Let's make it easy for us.
So do you know what a variable is? If you know
what a variable is, then you know already, what a pointer is? I am not joking.
Variable: A variable for us is just a name which
holds a value. Variable can change the value in a program, that's why it is
called a variable.
int sum;
It tells us that sum is a
variable which holds an integer value.
But how it is happening in our computer.Well, its all happening in memory of the computer. You can visualize the computer memory like below:
Address: :
|-----|
0x1100| | sum is an integer, one machine word big
|-----|
|-----|
0x1104| | ptr is a pointer, also one word big
|-----|
:
Don't get confused with "One machine word big". It means it depends on computer. In my case, One word means 4 bytes, so it is taking 4 memory space (1100 to 1103), each of one Byte.
So what we understood by:
int sum;
sum is a variable which holds an integer
OR
sum is a memory location in the computer and that memory location holds an integer.
so the above statement (int sum;) is telling the computer to reserve a memory location (when the program will execute) which will hold an integer.
Lets talk about the pointer now.
int *ptr;
This statement tells the computer that ptr is not a normal variable like sum in above example. Rather it is a pointer variable. A pointer variable always hold a memory address which is always a number.
What about the keyword int here.
So, above statements tell that ptr is a pointer variable (which holds a memory address) and it points to an integer. It means the memory address it is holding will be the memory address of an integer.
Lets make it simpler:
In the above example, Memory address 0x1100 is holding a variable sum.
sum is a memory location in the computer and that memory location holds an integer.
so the above statement (int sum;) is telling the computer to reserve a memory location (when the program will execute) which will hold an integer.
Lets talk about the pointer now.
int *ptr;
This statement tells the computer that ptr is not a normal variable like sum in above example. Rather it is a pointer variable. A pointer variable always hold a memory address which is always a number.
What about the keyword int here.
So, above statements tell that ptr is a pointer variable (which holds a memory address) and it points to an integer. It means the memory address it is holding will be the memory address of an integer.
Lets make it simpler:
In the above example, Memory address 0x1100 is holding a variable sum.
ptr is a pointer variable which
points to an integer, so it can point to sum. Right?
so if ptr want to point to sum,
it should hold the Memory address of sum. This means it should
hold 0x1100. Now assume that sum is 21
So,
So,
int sum=21;
int *ptr;
Above two statements will look like below in
memory
|---- -|
0x1100 | 21 | sum
|------|
|------|
0x1104 | | ptr is a pointer
|----- |
Now if we want that ptr should hold the memory
address of sum (which is an integer), how it will look in
memory:
0x1100 | 21 | sum
|------|
|----- |
0x1104 |0x1100| ptr is a pointer, also one word big
|----- |
All right ! You got it.
Now how to write it in code?
int sum=21;
int *ptr;
ptr = ∑
First two statements, we know. In third
statement, we are assigning the address of sum (using
& operator) into ptr. Since ptr can hold
the address only that why we assigned it an address, not the value.
Did you recall something? You have used the pointer already, but you don't know it. Remember scanf statement when you used to read the value of a variable using the & operator. You were doing nothing but reading the memory location. Make sense?
Now ptr is an address right? So what is the value at that address. You will say 21 right i.e. the value of sum. So you can directly print the value of sum in your printf statement. But can you tell the value at that address in terms of ptr. Yes, you can say it is *ptr
Did you recall something? You have used the pointer already, but you don't know it. Remember scanf statement when you used to read the value of a variable using the & operator. You were doing nothing but reading the memory location. Make sense?
Now ptr is an address right? So what is the value at that address. You will say 21 right i.e. the value of sum. So you can directly print the value of sum in your printf statement. But can you tell the value at that address in terms of ptr. Yes, you can say it is *ptr
So what did we learn?
ptr is a pointer which is holding a memory location. sum is a variable. Address of sum is &sum.
This address we have stored in a pointer called ptr. So ptr is also a memory address of sum means &sum and ptr is sum.
Value at the address represented by ptr is *ptr which is 21.
Think about the below statements:
printf("sum is %d.\n", *ptr);
and
printf("sum is located at %x.\n", &sum);
printf("sum is located at $%x.\n", ptr);
No comments:
Post a Comment