a pointer to a pointer to a ...

Hi,

I was just wondering how can I continue this code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int fun(int***** a)
{
	return *****a * 2;
}

int main()
{
	int a = 1;
	int* b = &a;
	int** c = &b;
	int*** d = &c;
	int**** e = &d;
	int***** f = &e;
	std::cout << fun(f) << "\n";
	std::cout << fun(&e) << "\n";

	system("PAUSE");
	return 0;
}


I mean it gives 2 at first that is pretty obvious, and gives 2 at the second, that is obvious too, but how do I continue? I mean can't do fun(&(&d)) and I can't do fun(&&d) either...
Can I do it over and over again? Something like &&&&n

Best regards,
Yours3!f
Last edited on
closed account (zb0S216C)
A pointer to a pointer to a pointer is rarely seen. Honestly, I've never seen a pointer to a pointer except in my own code. As for the rest of the pointer declarations, they're unnecessary.

Why do you want such pointer declarations anyway?

Wazzak
Number of stars is number of dynamic array, you can create.
For example with int** ptr; you can create a two dimensional array
For this reason, three stars and more will not be used usually.
Can I do it over and over again? Something like &&&&n
No. Under no circumstances can you apply the address-of operator twice in a row. This is because for &x, x must be an lvalue, but &x evaluates to an rvalue. It's the same reason why you can't do (x++)++.

Take a look at this:
1
2
3
4
5
void *p=&p;
std::cout <<p<<std::endl
    <<*(void **)p<<std::endl
    <<**(void ***)p<<std::endl
    <<***(void ****)p<<std::endl;
Think about it. &e get's the address to e, i.e. where it is stored in RAM. However, that address is a return value from the operator, asking where it is stored doesn't make any sense. It hasn't been stored anywhere, yet! It's similar to the reason you can't short cut your code above by doing this:
1
2
3
4
5
//the code
int a = 1;
int *b = &a;
//shortcut - will not work!
int *b = &1; //um... what? 
Last edited on
@Framework I just experimented with pointers
@majidkamali1370 I know they wont be used
@helios thanks I also noticed that it would be an rvalue I was just curious to see if there's any workaround.... I assume they all give back the address if p?
@Mathhead yeah you're right
thanks Mythios it looks like a very interesting application!
Is this the result you were looking for?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int fun(int***** a)
{
	return *****a * 2;
}

int main()
{
	int a = 1;
	int* b = &a;
	int** c = &b;
	int*** d = &c;
	int**** e = &d;
	int***** f = &e;
	std::cout << fun(&*f) << "\n";
	std::cout << fun(&e) << "\n";

	system("PAUSE");
	return 0;
}


I'm not sure I would use a linked list to reference pointers of pointers of pointers, but rather hold specific values for an indeterminable length of elements.
Last edited on
@mcrist no :) with that you basically did nothing, you made another pointer to it and you dereferenced that so you got back the original pointer f

if it would be possible (see helios' reply) I would've liked to know how can I do the same thing with d, c, b and a

something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

int fun(int***** a)
{
	return *****a * 2;
}

int main()
{
	int a = 1;
	int* b = &a;
	int** c = &b;
	int*** d = &c;
	int**** e = &d;
	int***** f = &e;
	std::cout << fun(f) << "\n";
	std::cout << fun(&e) << "\n";
        std::cout << fun(&&d) << "\n"; //error
        std::cout << fun(&&&c) << "\n"; //error
        std::cout << fun(&&&&b) << "\n"; //error
        std::cout << fun(&&&&&a) << "\n"; //error

	system("PAUSE");
	return 0;
}


so again it is not possible, only if you go through it with a function...
There IS a way to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int fun(int***** a)
{
	return *****a * 2;
}

int main()
{
	int a = 1;
	int* b = &a;
	int** c = &b;
	int*** d = &c;
	int**** e = &d;
	int***** f = &e;
	std::cout << fun(f) << "\n";
	std::cout << fun(&e) << "\n";
        std::cout << fun(&(&d, e)) << "\n";
        std::cout << fun(&(&(&c, d), e)) << "\n";
        std::cout << fun(&(&(&(&b, c), d), e)) << "\n";
        std::cout << fun(&(&(&(&(&a, b), c), d), e)) << "\n";

	cin.sync();
	cin.ignore();
	return 0;
}
It doesn't do what you want it to do, but it still looks cool. The compiler probably optimizes the left operands of the comma operator if they are useless, as in this case.
Last edited on
It doesn't do what you want it to do
Then it's not a way to do it.
Yours3lf wrote:
with that you basically did nothing,


Sums up this whole thread, I'd say ;P

ZING
Topic archived. No new replies allowed.