I have a question on the next "code", why do I pass to the function func the address of an address? (&var)
Thanks.Code:int func(int **); main() { int *var; func(&var); }
I have a question on the next "code", why do I pass to the function func the address of an address? (&var)
Thanks.Code:int func(int **); main() { int *var; func(&var); }
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
>why do I pass to the function func the address of an address?
http://faq.cprogramming.com/cgi-bin/...1043284351#016
[edit]
It's not "the address of an address", it's "the address of a (pointer) variable".
[/edit]
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
For a further illustration, look at this neighboring thread.
Quzah.
Hope is the first step on the road to disappointment.
the address of a pointer? but the address of *var is var
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
int var;
int *varptr;
int **varptrptr;
var = 5;
varptr = &var;
varptrptr = &varptr;
*varptr gives us 5.
*varptrptr gives us varptr.
**varptrptr gives us 5.
Dereferencing a pointer gives you whatever it points to.
Thus:
*varptr gives you the integer
*varptrptr gives you the pointer.
So **varptr gives you the pointer varptr which in turn is dereferenced, giving you the 5.
Quzah.
Hope is the first step on the road to disappointment.
so, by & on an address you get it's value?
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
The & gives you the address of that variable. (IE: Where that variable is in memory.)
Thus:
To make a pointer point at something, it either has to be a direct assignment to another pointer, or the address of a variable:Code:#include <stdio.h> int main( void ) { int x = 5; printf("%p is the address of x.\n", &x ); printf("%d is the value of x.\n", x ); return 0; }
Quzah.Code:int x; int *ptr1, *ptr2; x = 5; ptr1 = &x; /* make ptr1 hold the address of x */ ptr2 = ptr1; /* make ptr2 point at what ptr1 points at */
Hope is the first step on the road to disappointment.
I understand!
so &varptr is the address of *varptr, to match varptrptr's address, sort of speak...
Last edited by Devil Panther; 07-04-2003 at 03:56 PM.
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
You can use parenthesis to illustrate exactly what happens with the double-dereference:
int x;
int *xp;
int *xpp;
x = 5; /* give a value to x */
xp = &x; /* give a value to xp */
xpp = &xp; /* give a value to xpp */
The value of x is 5.
The value of xp is the address of x.
The value of xpp is the address of xp.
*xp /* dereferencing a pointer gives you the value of what it points to, thus 5. */
*xpp; /* dereferencing a pointer gives you the value of what it points to, thus the address of xp. */
**xpp can be expanded visually to:
*(*xpp)
Thus, *xpp is the what xpp points to, which is a pointer. So, dereferencing that pointer gives what that pointer points to, producing 5, which is the value of x.
One more time, "*xpp" gives you the pointer "xp". Dereferencing that pointer gives you x, and the value of 5. You can combine the two dereferences into a single step:
**xpp
Quzah.
Hope is the first step on the road to disappointment.
I understand!
so &xp is the address of *xp, to match xpp's address, sort of speak...
Also, can i do it directly from: int x to int **x ?
Thanks
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
But what I don't understand, is if int *xp, **xpp, why: *xpp = xp kills the program? it is support to be the same thing, no?
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
Try this to clear things up:
Perhaps that will help clear things up.Code:#include <stdio.h> int main( void ) { int x, *xp, **xpp; x = 5; /* give x a value */ xp = &x; /* give xp a value, the address of x. */ xpp = &xp; /* give xpp a value, the address of xp */ printf("A value is what a variable holds.\n"); printf("Pointers hold addresses of other variables as their value.\n"); printf("To get the address of a variable, use '&'\n"); printf("The value of 'x' is %d.\n", x ); printf("The address of 'x' is %p.\n", &x ); printf("The value of 'xp' is %p.\n", xp ); printf("The address of 'xp' is %p.\n", &xp ); printf("The value of 'xpp' is %p.\n", xpp ); printf("The address of 'xpp' is %p.\n\n", &xpp ); printf("Dereference a pointer to get what it points to.\n"); printf("To dereference a pointer, use '*'\n"); printf("Use the list of addresses above to see what is happening when you dereference pointers.\n\n"); printf("Dereferencing xp gives us: %p\n", *xp ); printf("Dereferencing xpp gives us: %p\n\n", *xpp ); return 0; }
[edit] Fixed up %p / %d issues as illustrated below. [/edit]
Quzah.
Last edited by quzah; 07-04-2003 at 08:26 PM.
Hope is the first step on the road to disappointment.
Code:printf("The value of 'xp' is %p.\n", xp ); printf("The value of 'xpp' is %p.\n", xpp ); printf("Dereferencing xpp gives us: %p\n\n", *xpp );
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
[edit]
Nevermind, I see what you were pointing out. That's what you ( I ) get for copy/pasting the printf lines.
[/edit]
Quzah.
Last edited by quzah; 07-04-2003 at 08:23 PM.
Hope is the first step on the road to disappointment.