That's very likely what I'm doing for my next development project as I don't have time to start over again.
Below shows the code so far after some thinking and collaborations on getting around my problem.
However, I'm wondering if I should make dayString a pointer instead of a char array and do this:Code:// This function converts day of the week in number to string void access_day(unsigned char number, char dayString[]) { // Declaration and Initialization of Days of the Week string char sundayArray[] = { 'S', 'u', 'n', 'd', 'a', 'y'}; char mondayArray[] = { 'M', 'o', 'n', 'd', 'a', 'y'}; char tuesdayArray[] = { 'T', 'u', 'e', 's', 'd', 'a', 'y'}; char wednesdayArray[] = { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y'}; char thursdayArray[] = { 'T', 'h', 'u', 'r', 's', 'd', 'a', 'y'}; char fridayArray[] = { 'F', 'r', 'i', 'd', 'a', 'y'}; char saturdayArray[] = { 'S', 'a', 't', 'u', 'r', 'd', 'a', 'y'}; // index lookup table of the day strings /*uint indexArray[] = {&sundayArray, &mondayArray, &tuesdayArray, &wednesdayArray, &thursdayArray, &fridayArray, &saturdayArray}; char* pDay = (char*) indexArray[0];*/ char* pIndexArray[] = {&sundayArray, &mondayArray, &tuesdayArray, &wednesdayArray, &thursdayArray, &fridayArray, &saturdayArray}; char* pDay = pIndexArray[number-1]; }
instead of:Code:dayString = pIndexArray[number-1];
to return the string of the actual day of the week rather than the number.Code:char* pDay = pIndexArray[number-1];
You're not allowed to return a pointer to a local non-static variable (they'll be destroyed when the function is returned).
You're over complicating this way too much,
Of course, you'll still need to add error checking -- and it's not perfect either.Code:/* returns 0 on success, else non-zero */ int accessDay(size_t n, char * day, size_t maxLen) { const char days[7][] = { "Sunday" "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; /* adjust n, since it's 1 based */ --n; /* error day is out of bounds */ if(n > (sizeof(days) / sizeof(days[0]))) return 2; strncpy(day, days[n], maxLen); /* etc */ return 0; }
True enough, that does the trick.
A little mistake on iMalc's part, then, I guess. Proper C89 code would be:
Code:void getDayOfWeekName(int inDayIndex, char *outDayName) { static const char* const days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; assert(1 <= inDayIndex && inDayIndex <= 7); assert(outDayName != NULL); strcpy(outDayName, days[inDayIndex - 1]); }
I dont know what asserts are but somehow this just looks wrong:
assert(1 <= inDayIndex && inDayIndex <= 7);
It's not. Just remove the assertion for a second and see for yourself:
Code:#include <assert.h> #include <stdio.h> #include <string.h> #define WYSIWYG(maybe) printf( #maybe##", %d\n", (maybe) ); void getDayOfWeekName( int inDayIndex, char *outDayName ) { static const char* const days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; WYSIWYG( ( 1 <= inDayIndex && inDayIndex <= 7 ) ) assert(outDayName != NULL); strcpy(outDayName, days[inDayIndex - 1]); } int main( void ) { int i; char p[10]; for ( i = 1; i <= 7; i++ ) { getDayOfWeekName( i, p ); } return 0; }
Last edited by whiteflags; 01-01-2008 at 02:08 PM.
I guess I confused things by putting that in there, and in the wrong place for C89.
I write it like this: 1 <= x && x <= 7 because it kinda looks like 1 <= x <= 7, which is pretty much mathematical syntax to show x being between 1 and 7 inclusive. Kinda nifty once you get used to it I think.
Feel free to write x >= 1 though instead of course.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
My preference would be to return a const char*, for two reasons:
1) any need for an strcpy() is defered to the caller
2) ability to indicate a bad param error condition (return NULL)
gg
I would also go with the return a const char* approach since it seems more flexible.
In many cases you might not want to reassign the actual string you get. You might want to compare it or such. Then a const char* is perfect.
However, if you really do need to modify the contents or use it in any way, you're free to do the copy yourself. Thus you may save time, as well. New function:
Code:const char* getDayOfWeekName(int inDayIndex) { static const char* const days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; assert(inDayIndex >= 1 && inDayIndex <= 7); return days[inDayIndex - 1]; }
Yup that's good too IMHO.
Using const char* as the return value is useful.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Is the assertion used for error-checking to ensure that the number is within range? It seems that mikroC doesn't support that.
I came up with a way of getting around it such as:
Please let me know what you think. Thanks.Code:const char* getDayOfWeekName(int inDayIndex) { static const char* const days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; if (!(inDayIndex >= 1 && inDayIndex <= 7)){ return "Error"; } return days[inDayIndex - 1]; }
Last edited by stanlvw; 01-02-2008 at 08:27 PM.
assert is a debug-only check that breaks you into the debugger at the point where the statement was false.
On an embedded system I guess you'd need to implement your own form of assert, that perhaps just logs the error. What you've written could be fine for you.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"