![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
neil
Joined: 08 Sep 2003 Posts: 128
|
Function pointer assignment back to front?? |
Posted: Tue Nov 14, 2006 11:12 am |
|
|
Hi again,
I am writing a menu for an LCD (please don't groan and stop reading yet!)
You know the story...
I couldn't get a pointer to function to work (using 3.249 or V4) so I wrote a test program. It won't compile in CCS but it works fine on a DEC terminal!
Code: | void (*funcptr) (void); // Declare a pointer to func with no parameters
void function1(){
printf("Function 1 selected\n\r");
}
void function2(){
printf("Function 2 selected\n\r");
}
main(){
while(TRUE){
funcptr = &function1; // Select function 1
funcptr(); // call it via the pointer.
delay_ms(1000);
funcptr = &function2;
funcptr();
delay_ms(1000);
}
}
|
The line "funcptr = &function1;" gives the error "Expecting an identifier" but on the DEC terminal it does exactly what is required.
More strangely, I have played about with different variations in syntax and found that the following code compiles (in CCS) and works as expected of the first code!
Code: | void (*funcptr) (void); // Declare a pointer to func with no parameters
void function1(){
printf("Function 1 selected\n\r");
}
void function2(){
printf("Function 2 selected\n\r");
}
main(){
while(TRUE){
*funcptr = function1; // What is going on here?!
(*funcptr)(); // call it via the pointer.
delay_ms(1000);
*funcptr = function2;
(*funcptr)();
delay_ms(1000);
}
}
|
Surely "*funcptr = function1;" is completely the wrong way to assign a function to the function pointer and should simply not work?
Neil. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Tue Nov 14, 2006 11:33 am |
|
|
The compiler is just ignoring the extra level of indirection.
Code: |
funcptr = function1; // Select function 1
funcptr(); // call it via the pointer.
|
Is normal syntax, and works.
Remember a function name, _is_ it's address. Normally, compilers are smart enough, to ignore the use of &, but CCS isn't at present, so your first syntax is asking for the address of an address, without allocating any storage for it (hence the error).
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
neil
Joined: 08 Sep 2003 Posts: 128
|
|
Posted: Wed Nov 15, 2006 5:15 am |
|
|
Hi, I understand that the & may be omitted and that CCS may still require it to be used. I actually prefer to use the & as it explains more when you glance at the code.
What I don't understand is how in real C a function pointer is assigned like this:
Code: | funcptr = &function1; |
and called like this:
or:
but CCS will only work when it is assigned like this:
Code: | *funcptr = function1; |
the first (correct) way assigns a function name (the address) to the pointer, but the second way is assigning a value 'function1' to the location that the pointer points to, which also happens to be void!!!!
One of the (proper) programmers I work with has looked at this and suggests that it is a compiler bug.
What do you guys think?
Neil. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Nov 15, 2006 6:21 am |
|
|
Read what I said.
CCS, _doesn't_ 'only work when it is assigned like this'. It works without the need for the * on the variable.
Now the point is that adding '&', _should_ be legal (it is exactly the same as on an array, where though the name is the address, you can add the &, and this is then ignored by the compiler). However on functions, this 'ignoring' doesn't work for CCS. Yes this is a bug.
Now on the use of the *, this in fact represents exactly the same 'ignoring', but working beyond the 'call of duty'!...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|