#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
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.
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++)++.
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?
@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
#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.
#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.