CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Function pointer assignment back to front??

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
neil



Joined: 08 Sep 2003
Posts: 128

View user's profile Send private message

Function pointer assignment back to front??
PostPosted: Tue Nov 14, 2006 11:12 am     Reply with quote

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.
Ttelmah
Guest







PostPosted: Tue Nov 14, 2006 11:33 am     Reply with quote

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
neil



Joined: 08 Sep 2003
Posts: 128

View user's profile Send private message

PostPosted: Wed Nov 15, 2006 5:15 am     Reply with quote

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:
Code:
funcptr();

or:
Code:
 (*funcptr)();


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.
Ttelmah
Guest







PostPosted: Wed Nov 15, 2006 6:21 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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