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

 
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-01-2008
ssylee wrote:
> Below shows the code so far after some thinking and collaborations on
> getting around my problem.
>
> // 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'};

etc.
These are not strings.
If you want strings, just use
char sunday[] = "Sunday";
etc.
If your compiler barfs on this, or if you are correct in your earlier
claim that
> 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" };

then you are not using a C compiler and the language you are writing is
not C. Perhaps someone in the embedded programming newsgroup or on some
newsgroup or mailing list for the not-C compiler you are using can help you.
 
Reply With Quote
 
 
 
 
ssylee
Guest
Posts: n/a
 
      01-01-2008
On Jan 1, 1:18 am, Martin Ambuhl <(E-Mail Removed)> wrote:
> ssylee wrote:
> > Below shows the code so far after some thinking and collaborations on
> > getting around my problem.

>
> > // 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'};

>
> etc.
> These are not strings.
> If you want strings, just use
> char sunday[] = "Sunday";
> etc.
> If your compiler barfs on this, or if you are correct in your earlier
> claim that> 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" };

>
> then you are not using a C compiler and the language you are writing is
> not C. Perhaps someone in the embedded programming newsgroup or on some
> newsgroup or mailing list for the not-C compiler you are using can help you.


Sorry for forgetting to quote the lines. I initially thought the
readers may be reading the previous messages on the thread. In the
meantime, I have posted a question on mikroC forums and hopefully will
get some help soon. Thanks for all your help.
 
Reply With Quote
 
 
 
 
Paul Heininger
Guest
Posts: n/a
 
      01-01-2008

"ssylee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Below shows the code so far after some thinking and collaborations on
> getting around my problem.
>
> // 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];
>
>
> }
>
> However, I'm wondering if I should make dayString a pointer instead of
> a char array and do this:
>
> dayString = pIndexArray[number-1];
>
> instead of:
>
> char* pDay = pIndexArray[number-1];
>
> to return the string of the actual day of the week rather than the
> number. Thanks!


How does your function know how many chars can fit into the passed-in
dayString buffer?
How does your function know how many chars to copy from the
pIndexArray[number-1] arrays?
How does your function return the number of chars that it filled into the
dayString?

I would use the advice that you have already been given about using pointers
to strings (character arrays that are nul char terminated). This is not
the only solution, but you have been given good advice.

Paul


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-01-2008
ssylee <(E-Mail Removed)> writes:
[...]
> 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" };

[...]

Really? When I compile a source file containing just that exact
declaration, I don't get any complaints (except "warning: `day'
defined but not used" with some options). As others have said, if
your compiler rejects it, then it's not a conforming C compiler.

Normally the size of the array is inferred from the number of elements
in the initializer. If your (non-conforming) compiler doesn't do
this, changing "char *day[]" to "char *day[8]" might be a workaround;
the resulting code would still be valid C. Consult your compiler's
documentation.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
merl the perl
Guest
Posts: n/a
 
      01-02-2008

"Martin Ambuhl" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

snip
Huh? The kid's asking for the real time.

Maybe the above might be a way to shoe-horn it into C. C can't find time
with both hands. If I had to write something sensitive in this respect I
would pass it to C by virtue of a language that is on a first-name basis
with computers who function as clocks.

I am also unschur of what a switch(variable ) ... case
>> x: .... structure.

is.
--
tja
and



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      01-02-2008
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. 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 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].

--
Army1987 (Replace "NOSPAM" with "email")
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-02-2008
merl the perl wrote:
> "Martin Ambuhl" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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.


[my reply snipped, being irrelevant to "merl the perl"'s response]

> snip
> Huh? The kid's asking for the real time.


There is nothing in the question about "real time". It is about a
function that will return a string representing the name of a day of the
week given a number in the range [1 ... 7]. If you cannot read
specifications better than that, go back to selling shoes.

> Maybe the above might be a way to shoe-horn it into C. C can't find time
> with both hands.


The above is, of course, uninformed flame-bait. Go back to selling shoes.

> If I had to write something sensitive in this respect I
> would pass it to C by virtue of a language that is on a first-name basis
> with computers who function as clocks.


And that's not even English. Maybe you can't sell shoes, either.

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-02-2008
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" };

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-02-2008
Martin Ambuhl <(E-Mail Removed)> 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. In fact, I'd declare it as

const char *const wdays[] = ...

Whether it should be static or not depends on how it's used. Using
NULL rather than the string "Error" as an error marker seems
reasonable.

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


Which is probably a flaw in the OP's compiler; I suggested changing
"[]" to "[8]" as a possible workaround.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-02-2008
Keith Thompson wrote:
> Martin Ambuhl <(E-Mail Removed)> 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.

 
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