|
-
March 23rd, 2009, 03:59 PM
#10
Re: pointer to pointer concept
 Originally Posted by Muthuveerappan
My initial Doubt
-------------------
1) When the address of k1 can be stored in a int* variable, why can’t the address of the pointer p1 be stored in another int* variable ?
It could. The below program works just like yours:
Code:
#include <iostream>
using namespace std;
int main()
{
int k1=20, *p1, *p2;
p1 = &k1;
p2 = (int*)&p1;
cout << "k1 =" << k1 << "\t\t&k1 =" << &k1 << endl
<< "*p1 =" << *p1 << "\t\tp1 =" << p1 << "\t\t&p1 =" << &p1 << endl
<< "**p2=" << **(int**)p2 << "\t\t*p2 =" << (int*)*p2 << "\t\tp2 =" << p2 << "\t\t&p2 =" << &p2 << endl;
}
However, why do you want to pretend that p2 is pointing to an integer while in fact p2 is pointing to an address. The compiler even warns you that your pointers are not correct, that's why I added the casts.
BTW, there is a concept of pointers that are not defining what they point to. They are defined as void *. The concept is used, when you have to store a memory address without knowing yet what exactly is at this memory, e.g. in network programming or inter-process communication.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|