Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 4th, 2005, 8:09 PM   #1
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
simple pointer array w/ function crash

Hey everyone,

I wrote a simple C program that asks you to enter a number b/w 1 and 7, and then is displays the day of week corresponding to this number. I.e - you enter 2, you get 'Monday'. Well, i'm thinking the problem is in the function, specifically the way i tried to return day of week. But i'm not really sure so i'm going to post the whole program. It crashes after a number is entered.

#include <stdio.h>
#include <stdlib.h>
char day(int);

int main()
{
    char *week,dayName;
    int n;
    
    printf("Please enter a number between 1 and 7.\n");
    scanf("%d", &n);
    
    while(n<1 || n>7)
    {
           printf("Please retry using a number between 1 and 7.\n");
           scanf("%d", &n);
    }
    
    dayName=day(n);
    printf("The # %d corresponds to %s.\n",n,dayName);
    
    system("PAUSE");
    return 0;
    
}
char day(int n)
{
     n=n-1;
     char *week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
     return *week[n];
}

Does anyone see where i went wrong.
Thanks
codylee270 is offline   Reply With Quote
Old Dec 4th, 2005, 8:40 PM   #2
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
You are returning a single char. For example, when the user chooses 2, 'M' is returned. printf wants a string and chokes. Change your function declaration and definition as such:

char* day( int n );

.....



char* day( int n );
{
     char *week[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
     return week[ n - 1 ];
}
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Dec 4th, 2005, 8:43 PM   #3
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,183
Rep Power: 5 lectricpharaoh will become famous soon enough
What stevengs said. However, you'll also need to make that array inside day() a static one. If you don't, the memory for the array will be reclaimed from the stack when the function exits, and you will be returning a pointer to this newly-unallocated space. This can cause whole heaps of trouble.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Dec 4th, 2005, 9:02 PM   #4
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
well,
i changed all of that but now it says i have an issue with

dayName=day(n);

says something about "Invalid conversion from 'char*' to 'char'."
What is this?
codylee270 is offline   Reply With Quote
Old Dec 4th, 2005, 9:10 PM   #5
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
right, forgot that bit. You declared dayName as a char, but you are assigning to it the char* that is returned from day(). You can drop week, as it is not used anywhere, changing the declaration line to:

char* dayName;

make sure your types match, otherwise you will be relying on implicit conversions which can result in surprises, or the compiler will simply bail.
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Dec 4th, 2005, 9:15 PM   #6
robrosas
Newbie
 
robrosas's Avatar
 
Join Date: Nov 2005
Location: Puerto Rico
Posts: 12
Rep Power: 0 robrosas is an unknown quantity at this point
....

Just declare dayName as
 char* dayName;
__________________
From P.R. to the World!! RoBeRt
robrosas is offline   Reply With Quote
Old Dec 4th, 2005, 9:17 PM   #7
robrosas
Newbie
 
robrosas's Avatar
 
Join Date: Nov 2005
Location: Puerto Rico
Posts: 12
Rep Power: 0 robrosas is an unknown quantity at this point
Ooops sorry didnt grabbed that last post...
__________________
From P.R. to the World!! RoBeRt
robrosas is offline   Reply With Quote
Old Dec 4th, 2005, 9:20 PM   #8
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
Since * in variable declarations are bound to the variable name and not the base type, this line:
char *week,dayName;
makes week a char* and dayName a char. So change that line to:
char *week, *dayName;
Kaja Fumei is offline   Reply With Quote
Old Dec 4th, 2005, 9:28 PM   #9
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
Great, makes sense, and works wonderful.. Thanks..
codylee270 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC