recursive functions with *pointer parameters

Here is the code I made... but I don't understand what going on in there..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

long long unsigned factorial(long long unsigned int *n) {
	return *n<=1 ? 1 : (*n)*factorial(&(*n)-1);
}

int main() {

	long long unsigned n=5;
	cout << factorial(&n);

return 0;
}

Output is everytime 0.
0

So what happens when you use a recursive function with a pointer as a parameter?
Also very unsure about this part:
(*n)*factorial(&(*n)-1);
Doesn't that mean that it modifies the address of the *n ? Very confused.. help please :(
The address of operator has greater precedence than - (minus) so

factorial(&(*n)-1);
means take the address of (*n) and than subtract.

Moreover you can't use additional parenthesis for clarification

factorial(&((*n)-1));
because expression ((*n)-1) is not an l-value and & (address of) requires an l-value.

I think you can't use conditional operator in this case.

The correct code could be:

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

using namespace std;

long long unsigned factorial(long long unsigned int * n) {
	if (*n<=1) return 1;
	else { long long unsigned x=(*n)-1; return ((*n) * factorial(&x)); }
}

int main() {

	long long unsigned n=5;
	cout << factorial(&n);

return 0;
}
why doesn't this work ?

1
2
3
4
5
6
7
long long unsigned factorial(long long unsigned int * n) {
	if (*n<=1) return 1;
	else {
		(*n)--;
		return ((*n) * factorial(&*n));
	}
}

Output everytime:
1

can it be done without a extra variable ?
Last edited on
factorial(&*n)
is a nonsense from my point of view.

n - is a pointer
*n - is the value pointed by n
&*n - is address of the value pointed by n, so it's in fact n

I don't see any problem in using an extra variable.
thanks
There is a reason why this always returns 1.

think about it very very carefully..in particular what is actually happening with this statement.

1
2
3
4
5
6
7
long long unsigned factorial(long long unsigned int * n) {
	if (*n<=1) return 1;
	else {
		(*n)--;//This statement here .......
		return ((*n) * factorial(n));
	}
}
well... that decrements the value that the pointer n is pointing to... that being the n in the main function, actually in the function that called him. But I still can't tell why it's always 1.
Shouldn't the values be saved on the stack at each call? ... cause at each unwind... n remains 1.
Last edited on
why is this function taking a pointer in the first place? It makes much more sense to pass n by value here.
yeah... I know. Someone in class today wanted to try it with a pointer... and didn't work for him.. so I said i'll give it a shot too... Also the prof. took a shot at the program too but couldn't make it work...
So i tried to make it and didn't work... and I just want to get more knowledge on pointers and how they work.. by understanding what is happening in there.
1
2
3
4
5
6
7
8
9
long long unsigned factorial(long long unsigned int * n)
{
	if (*n<=1) return 1;
	else 
    {
		(*n)--;
		return (int( *n +1) * factorial(n));
	}
}
amazing... it works. Brb i'll just go ahead and debug it to see what happens :D .

edit: I can't figure out what's going on there... what exactly happens when you typecast that return value?
Does the value returned gets saved on the stack if it's typecasted ?
Last edited on
It's not the return value that is being typecasted -it is not even a typecast. It is the creation of an temporary unamed integer variable.

The problem with the original code was that each call of the function was not remembering the
value of *n that it was called with. It always ended up using the final value of *n which our result was always 1 or 0 (depending on how it was written).

We solve this problem by creating an unamed temporary stack variable int( *n +1) to
store the current value of *n (we use *n+1 because we had decremented *n just the line before)
Last edited on
Oh... thanks! :)
Topic archived. No new replies allowed.