Re: function returning days of the week



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



Relevant Pages

  • Re: xPCFileSystem.ReadFile function xpctarget
    ... union of a character array and a float or double. ... As to how you need to deal with a VARIANT output, ... VARIANT ReadFile(int fileHandle, int start, int numbytes); ... I dont understaind the return of the function, ...
    (comp.soft-sys.matlab)
  • Re: Use of Long and Long Long
    ... intuitive and much more useful (as it is now, ... However it's not impossible either for the compiler to impose 8, 16, 32, 64-bit word sizes. ... but on a significant number of main stream DSP processors using anything less than the size the processor understands ... On any given processor, types that are too small are inefficient because they must be emulated by bit-masking operations, while types that are two big are inefficient because they must be emulated using multiple instances of the largest efficient type. ...
    (comp.lang.c)
  • Re: A little one...
    ... Sava Mikalački wrote: ... > Hi to all of you god programmers in the world! ... > int main ... Read into a character array and use sscanf. ...
    (comp.lang.cpp)
  • Re: Use of Long and Long Long
    ... intuitive and much more useful (as it is now, changing int x to long ... On any given processor, types that are too small are inefficient because they must be emulated by bit-masking operations, while types that are two big are inefficient because they must be emulated using multiple instances of the largest efficient type. ... If signed char, short, int, long and long long were all required to have different sizes, a processor with hardware support for fewer than 5 different sizes would have to choose one form of inefficiency or the other; efficient implementation of all 5 types would not be an option. ...
    (comp.lang.c)
  • Re: explanations about the Decorator design pattern
    ... > abstract int doAnotherTask(); ... > class Inefficient extends SomeClass { ... > int doSomeTask() { ...
    (comp.lang.java.programmer)