By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,601 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,601 IT Pros & Developers. It's quick & easy.

Cheating C

P: 7
I am trying to write a single function that can handle a string or number as one of its input parameters. In an ideal situation this might be

at_wr("CH","text string");
at_wr("NI",0x234);

This is to be used on a Hayes type modem AT command set. There are various commands CH,NI,ID,PL etc followed by a text string,or a 16bit or 32bit number. I was thinking that from the commands CH for instant, I know (and the compiler can know) that the argument following is a text string. Similarly NI is always followed by a number. I know I can do something like

at_wr_str("CH","text string");
at_wr_u16("NI",0x2345);
at_wr_u32(" etc....

ie. make different functions, but is there any other way of telling or tricking the compiler to accept my 1st example. I looked at unions, macros, thought about using pointers. C++ seems to have the answer with the ability to overload a function name so the complier matches the function definition to the arguments.

I am a bit stumped, but I thought I would just ask before resorting to writing the 3 or 4 different function names I need. One function would be more elegant..
Nov 11 '06 #1
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 1,510
why not use overloaded functions in C++? i.e. two different functions can have the same name if their parameter types or number are different. see
http://www.cplusplus.com/doc/tutorial/functions2.html
Nov 11 '06 #2

P: 7
Yes, but I'm using a Keil C compiler for an ARM microprocessor and I dont think C++ is an option, I wish it were. I am pretty new to C and am just hoping there is a way of doing what I would like to do.

Maybe I could treat everything as a text string and convert as required ie.

at_wr("CH","text string");
at_wr("NI","0x234");
Nov 11 '06 #3

Expert 100+
P: 1,510
Yes, but I'm using a Keil C compiler for an ARM microprocessor and I dont think C++ is an option, I wish it were. I am pretty new to C and am just hoping there is a way of doing what I would like to do.

Maybe I could treat everything as a text string and convert as required ie.

at_wr("CH","text string");
at_wr("NI","0x234");
I thought that the Keil development environment for the ARM supports C and C++ - try a simple program and see.
Nov 11 '06 #4

P: 7
Your right, but no one seems to have tried it, I asked the question of the Keil Forum and .....nothing. I will try a "hello world".
Nov 11 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
Use a function with a variable parameter list (like printf)

void at_wr(char *cmd, ... );

use the macros va_start, va_arg, va_end and the type va_list defined in stdarg.h to access the data following command in the function call.

The only requirement is that whatever cmd points to must identify the type of the following parameters.
Nov 11 '06 #6

P: 7
Thanks. I tried a simple C++ in Keil and it doesn't work so I posted a question in Keil forum to ask if anyone has tried it yet.

Banfa - you read my mind, I am trying to get a simple version working right now.
Nov 12 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.