#include<stdio.h> void fibonacci(int *p,int n) { *p++=1; *p++=1; while(n>2) { *p++=*(p-1)+*(p-2); n--; } } void main() { int i,f[20]; fibonacci(f,20); for(i=0;i<=19;i++) printf("%d\n",f[i]); }
Ask big brother to help me explain this code, in the middle of * p ++ = 1 What does it mean? ? ? ? _ Mukeyuanwen https://www.imooc.com/wenda/detail/351407?t=246589
Answer 5
* P + operators have a problem of the order of operations, and * ++ operator first peer, but binding is from right to left, the first execution after the execution ++ * P
So * p ++ = 1 namely:
P points to the address pointer is incremented by one (or p The pointer points to the original address space), then it points to an assigned address space, a complete address pointer for this operation.
You adopt that answer some questions.
In fact, the post increment operator precedence is p ++ dereference operators than the *. In that figure he gave, the post-increment operator should be the first priority in the classification.
In this case, seems to explain the difference is not much, but in fact increment operator is a little complicated to explain:
1. The operation is the first ++: p is obtained corresponding to the pointer.
2. The operator then dereferencing operator: to give the object (i.e. an object when referred to the start p) obtained in the previous step the pointer is pointing.
3. Finally, a numerical assignment operator: the step 1 on the object assigned to that obtained p referred.
And because the increment operator will make the increment p, so p after the statement affirmed increment, that is, will point to an object behind.
Note that the one I said was, "p after the statement affirmed increment," I did not say "p incremented after the end of the statement" kind of thing, that p is incremented specific point in time at which It can not be determined. For this statement, p can be determined only after the end of the statement must have been a self-energizing.
Perhaps you will say, since it is uncertain when the increment of p, p perhaps before step 2 on the increment, then step 2 dereference may also be post-increment p yet?
Indeed p increment timing uncertainty, may also be incremented in step 2 above. But and 2 dereference operator does not matter - because there is not dereference the pointer p!
I said in the description of Step 1 is "the equivalent of p pointer", did not say "get a pointer p." Because the p ++ operator was actually obtained value of p, p not get the pointer itself. Or you can think of p ++ get is a temporary pointer, but the pointer and p is equal to it. So what if the subsequent increase of p from behind nothing to do with the operation.
C language and some operators get a "value of the object," and some operators get is "the object itself", please try to figure out the difference between these two good argument. Step 2 Solutions of such references operator, it is the object indicated by the pointer obtained itself, so that the step 1 is assigned objects.
So the answer to your adopted in "The pointer p still points to the original address space", that is in fact wrong! In fact it can not determine whether p then increment before.
You code is still a big question:
* P ++ = * (p-1) + * (p-2); the behavior of this statement is undefined!
He said the same reason as above, because the left side of the equal sign there are p ++ expression, which makes p increment, increment but timing is uncertain, p may be incremented when any step in the execution of this statement, so wait those right number p-1 p + 2 p values in the calculation of how much is uncertain. Then the whole expression is undefined!
p is an address, * p is an integer pointing to the address, meaning that the value after the assignment * p * p 1 + to
Look at the C language operator precedence table
P pointer points to the first address plus 1, then assign the value it points to a pointer to the changed one, this operation is completed, to the next address.