function returning days of the week

K

Keith Thompson

Martin Ambuhl said:
Keith said:
Martin Ambuhl said:
Army1987 wrote:
You don't even need a function.
const char *wdays = { NULL, "Sunday", "Monday" /*etc.*/ }
and you can use wdays. Or even, throw away the NULL and use wdays[i-1].
That's wrong. And the correct declaration


Yes, it needs to be "const char *wdays[]".
And the correct declaration
static char *day[] =
{ "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"

It's *a* correct declaration. Once the "[]" is added, I fail to see
why your declaration is more correct.


You yourself have noted that "const char *wdays" is wrong and "const
char *wdays[]" is correct. I fail to see how in the world you can be
obtuse enough not to see that mine is more correct than Army1987's.

A more interesting question is why Army1987 would post such an obvious
error three days after more correct (despite your inconsistent claim
that you can't see why it is) answer had been posted.


Read again. See where I wrote 'Once the "[]" is added, ...'?

Of course Army1987's declaration is incorrect, and yours is more
correct. I was comparing *a corrected version of* Army1987's
declaration to your declaration.
 
K

Keith Thompson

Martin Ambuhl said:
Keith said:
Read again. See where I wrote 'Once the "[]" is added, ...'?

Read again. I quoted my three-days earlier (and correct) reply. I
made no attempt to "correct" Army1987's gratuitously posted error.
Of course Army1987's declaration is incorrect, and yours is more
correct. I was comparing *a corrected version of* Army1987's
declaration to your declaration.

And I never claimed that my declaration was better than yours. There
are good reasons to prefer either. Why are you picking stupid fights
over nothing? Why suggest that I had claimed my answer was better
than yours, when I made no such claim. I could hardly do so, since
your claim that I had claimed mine was better than yours appeared
three days after my posting, before you had typed a character of your
reply.

The declaration I was comparing to yours wasn't exactly mine; it was
merely Army1987's incorrect declaration with the obvious fix. I
*thought* that that was what you were referring to.

Here's your article to which I replied:

| Army1987 wrote:
|
| > You don't even need a function.
| > const char *wdays = { NULL, "Sunday", "Monday" /*etc.*/ }
| > and you can use wdays. Or even, throw away the NULL and use wdays[i-1].
|
| That's wrong. And the correct declaration
| static char *day[] =
| { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
| "Thursday", "Friday", "Saturday"
| when suggested by me three days ago was met with the reply
| > Martin Ambuhl: The compiler is complaining about "Too many
| > initializer" whenever I tried compiling lines like:
| > static char *day[] =
| > { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
| > "Thursday", "Friday", "Saturday" };

Re-reading it, I think I misinterpreted what you wrote. Without going
back to re-read the previous context, I took your declaration as a
correction to Army1987's declaration. Adding the "[]" is of course
necessary, but dropping "const", adding "static", and changing NULL to
"Error" seemed odd. Now that I realize that you were quoting
something that had already been posted several days earlier, it all
makes sense.

This was a simple honest misunderstanding on my part, for which I
apologize. I certainly had no intention of "picking stupid fights".
 
S

ssylee

Martin Ambuhl said:
Keith said:
Read again. See where I wrote 'Once the "[]" is added, ...'?
Read again. I quoted my three-days earlier (and correct) reply. I
made no attempt to "correct" Army1987's gratuitously posted error.
And I never claimed that my declaration was better than yours. There
are good reasons to prefer either. Why are you picking stupid fights
over nothing? Why suggest that I had claimed my answer was better
than yours, when I made no such claim. I could hardly do so, since
your claim that I had claimed mine was better than yours appeared
three days after my posting, before you had typed a character of your
reply.

The declaration I was comparing to yours wasn't exactly mine; it was
merely Army1987's incorrect declaration with the obvious fix. I
*thought* that that was what you were referring to.

Here's your article to which I replied:

| Army1987 wrote:

|
| > You don't even need a function.
| > const char *wdays = { NULL, "Sunday", "Monday" /*etc.*/ }
| > and you can use wdays. Or even, throw away the NULL and use wdays[i-1].
|
| That's wrong. And the correct declaration
| static char *day[] =
| { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
| "Thursday", "Friday", "Saturday"
| when suggested by me three days ago was met with the reply
| > Martin Ambuhl: The compiler is complaining about "Too many
| > initializer" whenever I tried compiling lines like:
| > static char *day[] =
| > { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
| > "Thursday", "Friday", "Saturday" };

Re-reading it, I think I misinterpreted what you wrote. Without going
back to re-read the previous context, I took your declaration as a
correction to Army1987's declaration. Adding the "[]" is of course
necessary, but dropping "const", adding "static", and changing NULL to
"Error" seemed odd. Now that I realize that you were quoting
something that had already been posted several days earlier, it all
makes sense.

This was a simple honest misunderstanding on my part, for which I
apologize. I certainly had no intention of "picking stupid fights".

--
Keith Thompson (The_Other_Keith) <[email protected]>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


I've got a suggestion using the code below:

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];
}

However, I've come across another problem of not being able to use
assert. Therefore, I've come up with a method that gets around using
assert, but not sure if it's appropriate:

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];
}

Please let me know what you think. Thanks.
 
I

Ivan Novick

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];

}

However, I've come across another problem of not being able to use
assert. Therefore, I've come up with a method that gets around using
assert, but not sure if it's appropriate:

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];

}

If you really want to go this way then you should return 0 and not
"Error". Otherwise the user of your function has to do a strcmp to
determine if there was an error which is very expensive.

Regards,
Ivan Novick
http://www.0x4849.net
 
S

ssylee

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];

However, I've come across another problem of not being able to use
assert. Therefore, I've come up with a method that gets around using
assert, but not sure if it's appropriate:
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];

If you really want to go this way then you should return 0 and not
"Error". Otherwise the user of your function has to do a strcmp to
determine if there was an error which is very expensive.

Regards,
Ivan Novickhttp://www.0x4849.net


Thanks for the insight Ivan. It's not like I would like to do it this
way, but it doesn't appear that I have much of a choice either.
 
K

Keith Thompson

ssylee said:
I've got a suggestion using the code below:

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];
}

However, I've come across another problem of not being able to use
assert. Therefore, I've come up with a method that gets around using
assert, but not sure if it's appropriate:

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];
}

Please let me know what you think. Thanks.

What do you mean by "not being able to use assert"?

If an out-of-range argument is a coding error, then assert() is
probably appropriate. If it's something that can legitimately happen
(say, due to incorrect user input), then you need to handle it less
drastically.
 
S

slingerland3g

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

Please also see a similar forum posting back in 2004!

http://cboard.cprogramming.com/showthread.php?t=97432
 
J

J. J. Farrell

ssylee said:
I've got a suggestion using the code below:

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];
}

However, I've come across another problem of not being able to use
assert. Therefore, I've come up with a method that gets around using
assert, but not sure if it's appropriate:

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];
}

Please let me know what you think. Thanks.

I think your compiler is complaining about "Too many initializer". If it
isn't, what was wrong with Martin Ambuhl's considerably more elegant
version of the same solution, which he posted on 12/31?
 
S

ssylee

ssylee said:
I've got a suggestion using the code below:
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];
}
However, I've come across another problem of not being able to use
assert. Therefore, I've come up with a method that gets around using
assert, but not sure if it's appropriate:
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];
}
Please let me know what you think. Thanks.

I think your compiler is complaining about "Too many initializer". If it
isn't, what was wrong with Martin Ambuhl's considerably more elegant
version of the same solution, which he posted on 12/31?

I eventually used the suggestion in the cboard, and modified it to
make it work with the software project.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top