Code:#include <assert.h>
Already did that.
Aaargh, we're going horribly backwards here!
strftime - okay so it's not supported. That's a shame, but never mind.
stanlvw: Why on earth did you write such an abomination when the code I gave you was absolutely ideal? You don't have to specify strings arrays by individually specifying each character constant individually. And then you certainly don't have to declare 7 pointers to those 7 character arrays. What I wrote does exactly the same thing but with far less code and in a much more efficient way.
If you don't understand how it worked then just ask.
zacs7, even your code overcomplicates things a little, and has the same array declaration bug that pankaj401 originally posted with the array index. You'll notice it wont compile.
Are you sure you want to introduce runtime error handling to this? I think an assert is much better in this case.
Elysia: That isn't really an improvement in most cases. Sure if all you want is a pointer to a constant day of week name string then that will work. But most likely you want to place that piece of text in some other string, such as to build up a date string. In that case you would need to add a strcpy outside this function in every place you use it. In my code the strcpy is done in one place inside the function.
If you're unfamiliar with asserts, then I strongly suggest you learn about and use them.
The C89 rules about variable declarations and where they are placed still apply in the case of macros like assert.
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:
Quote:
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;
}
Oh yeah I'm dumb >_<
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 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.
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.
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];
}