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 :(
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.
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.
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 ?
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)