Re: function returning days of the week
- From: Martin Ambuhl <mambuhl@xxxxxxxxxxxxx>
- Date: Mon, 31 Dec 2007 14:04:20 -0500
ssylee wrote:
I need to write a function that would read in a byte that would return
a number between 1 to 7, 1 being Sunday, 2 being Monday, etc. I want
to return an actual string that says "Sunday", or "Monday", etc.
corresponding to the number. I know that the best method to implement
a lookup conversion table would be using switch(variable ) ... case
x: .... structure.
How do you "know" this? I think it is just flat wrong. Check the following code:
#include <stdio.h>
char *day_byte_to_str(int x)
{
static char *day[] =
{ "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
return (x < 1 || x > 7) ? day[0] : day[x];
}
int main(void)
{
int which;
printf("Test of day_byte_to_str()\n");
for (which = 0; which < 9; which++)
printf("%d -> %s\n", which, day_byte_to_str(which));
return 0;
}
[Output]
Test of day_byte_to_str()
0 -> Error
1 -> Sunday
2 -> Monday
3 -> Tuesday
4 -> Wednesday
5 -> Thursday
6 -> Friday
7 -> Saturday
8 -> Error
However, I may need to pass an array as one of the
parameters in order to access the text itself. Is there anything
inefficient in passing a character array as a parameter based on
memory consumption on an embedded microprocessor system? Thanks.
You pass the address of the array, not the array. This is basic stuff.
That is cheap. Copying a string literal into the array may have some small cost, but I can't believe that it is anything significant. If this is the greatest inefficiency in your program (and I doubt that considering what you "know"), then you have a very tight program indeed.
.
- References:
- function returning days of the week
- From: ssylee
- function returning days of the week
- Prev by Date: Re: Table based programming.
- Next by Date: Re: Table based programming.
- Previous by thread: Re: function returning days of the week
- Next by thread: Re: function returning days of the week
- Index(es):
Relevant Pages
|