Monday, May 30, 2005

[RFC] How many addresses can you take in C?

Well, I decided that I would post a question and ask for comments. The question is

In the programming language "C", how many times can you take the address of a variable v? So if v is a variable can I use &v, &&v, &&&v, etc? What is the limit to taking addresses?

I think I know the answer and it seems straight forward. Once, I get comments, I will try and illustrate the answer

6 comments:

Gops said...
This comment has been removed by a blog administrator.
Balbir said...

Gops had the correct answer. Once!

H said...

Indirectly you can do something like this :-

int i;
int *j;
int **k;
and so on;

&i
j=&i;&j
k=&j;&k and so on

Balbir said...

Yes, thats correct. But the fact than you can take one address at a time still remains. To give a simple answer

& is the address operator
&& is the and operator

Anonymous said...

Won't it be infinite if use paranthesis, thusly : &(&(&(&var))) ?

-Vinay

Balbir said...

Yes, Vinay, good point. We can use &(&a)), etc. But &(&(...)) is not a common operation as common as **...

For a given variable a on &a exists.
If int *p = &a, then &(&a) is not &p since "C" has no way of knowing all variables that could contain a pointer to a.

Ranking and Unranking permutations

I've been a big fan of Skiena's Algorithm Design Manual , I recently found my first edition of the book (although I own the third ed...