Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function returning days of the week

Reply
Thread Tools

function returning days of the week

 
 
Keith Thompson
Guest
Posts: n/a
 
      01-02-2008
Martin Ambuhl <> writes:
> Keith Thompson wrote:
>> Martin Ambuhl <> writes:
>>> Army1987 wrote:
>>>> You don't even need a function.
>>>> const char *wdays = { NULL, "Sunday", "Monday" /*etc.*/ }
>>>> and you can use wdays[i]. 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.

--
Keith Thompson (The_Other_Keith) <kst->
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-02-2008
Martin Ambuhl <> writes:
> Keith Thompson wrote:
>> 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[i]. 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) <kst->
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
ssylee
Guest
Posts: n/a
 
      01-03-2008
On Jan 2, 2:28 pm, Keith Thompson <ks...@mib.org> wrote:
> Martin Ambuhl <mamb...@earthlink.net> writes:
> > Keith Thompson wrote:
> >> 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[i]. 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) <ks...@mib.org>
> [...]
> "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.
 
Reply With Quote
 
Ivan Novick
Guest
Posts: n/a
 
      01-03-2008
On Jan 2, 6:26 pm, ssylee <staniga...@gmail.com> wrote:
> 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
 
Reply With Quote
 
ssylee
Guest
Posts: n/a
 
      01-03-2008
On Jan 2, 6:58 pm, Ivan Novick <i...@0x4849.net> wrote:
> On Jan 2, 6:26 pm, ssylee <staniga...@gmail.com> wrote:
>
>
>
> > 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-03-2008
ssylee <> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) <kst->
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
slingerland3g
Guest
Posts: n/a
 
      01-03-2008
On Dec 31 2007, 1:10*am, ssylee <staniga...@gmail.com> 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. 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
 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      01-04-2008
ssylee wrote:
>
> 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?
 
Reply With Quote
 
ssylee
Guest
Posts: n/a
 
      01-04-2008
On Jan 3, 9:32 pm, "J. J. Farrell" <j...@bcs.org.uk> wrote:
> ssylee wrote:
>
> > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calender control, how to display week days as English even windows region is Chinese? Jack ASP .Net 2 10-12-2005 01:28 AM
the penultimate week and last week of data for each month SimonC Javascript 13 01-04-2005 10:20 PM
ASP.NET: Day / Work Week / Week / Month web calendar control with view like MS Outlook ASP .Net Web Controls 3 12-22-2003 10:42 PM
ASP.NET: Day / Work Week / Week / Month web calendar control with view like MS Outlook ASP .Net 3 12-22-2003 10:42 PM
ASP.NET: Day / Work Week / Week / Month web calendar control with view like MS Outlook ASP .Net Building Controls 3 12-22-2003 10:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57