We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
In below code,I think pointer pexam->pf and pf1 will be same, but why pexam->pf++ can't change? What mistake i make? Can someone will help me. thx!
/////////////////////////// struct tagEXAM { int x; void (**pf)(void); }; void fun1(void); void fun2(void); void (*code pf[])(void)= { fun1,fun2 }; struct tagEXAM code exam[] = { {1, pf} }; int x; void (**pf1)(void); struct tagEXAM* pexam; void main(void) { while (1) { pexam = exam; pf1 = pexam->pf;
(*pexam->pf)(); (*pf1)();
pexam->pf++; pf1++; (*pexam->pf)(); (*pf1)(); } } void fun1(void) { x = 1; } void fun2(void) { x = 2; }
struct tagEXAM { int x; void (**pf)(void); }; void fun1(void); void fun2(void); void (*code pf[])(void)= { fun1,fun2 }; struct tagEXAM code exam[] = { {1, pf} }; int x; void (**pf1)(void); struct tagEXAM* pexam; void main(void) { while (1) { pexam = exam; pf1 = pexam->pf; (*pexam->pf)(); (*pf1)(); pexam->pf++; pf1++; (*pexam->pf)(); (*pf1)(); } } void fun1(void) { x = 1; } void fun2(void) { x = 2; }
While the choice of a '51 target is questionable when using function pointers, there doesn't appear to be anything particularly wrong with your code.
What do you mean by "can't change"?
You are trying to increment a pointer that is located in ROM. That's why it won't increment.
You are trying to increment a pointer that is located in ROM.
Well spotted.
struct tagEXAM code exam[] = { {1, pf} };
Now I make a little sense, because the struct tagEXAM* pexam is only a pointer for definition of struct tagEXAM, pexam->pf is not a pointer, it's a reference for function pointer fp[], it's located in ROM, so you can't increment it!. Am I right? confuse me, Can someone make a little clear? thx.