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

Replacing fputc() with custom function

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



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

Replacing fputc() with custom function
PostPosted: Thu Jun 01, 2006 7:24 am     Reply with quote

Hi,

I'd like to replace fputc with a custom function that calls my own routines for IO. My understanding is that when I do this fprintf() etc should make use of it as well.

I'm creating my own fputc but the compiler seems to be linking in its own version, not mine. Is it possible to do this with CCS? My compiler version is: 3.245

cheers

ed
Ttelmah
Guest







PostPosted: Thu Jun 01, 2006 7:42 am     Reply with quote

Don't try to use names that are already in use. In C++, you can do this, 'overriding' the existing function, but in C, this is not allowed.
I can't actually see any reason why you'd need to do this, for what you describe!. If you want to print to your own function, then use code like:
Code:

void my_putc (int val) {
   //output value here
}

printf(my_putc,"This will be sent to your function/n");



Best Wishes
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

Cheers
PostPosted: Thu Jun 01, 2006 8:28 am     Reply with quote

Hi,

Cheers for the tip I didn't realise you could do that. I'm sure you can do this kind of function replacement with C as I've done it before on an ARM processor. Its not about C++ style function overloading but about the compiler choosing to use the last instance of the function it sees rather than the original one.

I'll try the trick with printf.

cheers

ed
Ttelmah
Guest







PostPosted: Thu Jun 01, 2006 9:25 am     Reply with quote

OK. Yes, you should be able to do this with a true 'function', just as with a variable, where the 'local' copy should have priority over a version defined further out. However two problems arise. A lot of the CCS 'functions', are actually macros, rather than functions, and therefore require redefinition using the define ability, not a function name, and all of the 'library' functions, are defined at the same level as the normal function definitions (there is not a linker - yet...), which is normally required for true redefinition like this. Basically, in a 'linked' compiler, the external functions, are linked, when a local version does not already exist. CCS, is not a 'linked' language at present.
So you can redefine fputc, if required, with:

#define fputc(x,y) myfunc(x,y)

However the printf ability, is usually easier. :-)

Best Wishes
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

cheers Ttelmah
PostPosted: Thu Jun 01, 2006 9:30 am     Reply with quote

cheers Ttelmah, what a great answer!

ed
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