![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#3 |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,183
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#4 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Nov 2005
Location: Puerto Rico
Posts: 12
Rep Power: 0
![]() |
....
Just declare dayName as
char* dayName;
__________________
From P.R. to the World!! RoBeRt |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Nov 2005
Location: Puerto Rico
Posts: 12
Rep Power: 0
![]() |
Ooops sorry didnt grabbed that last post...
__________________
From P.R. to the World!! RoBeRt |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
Since * in variable declarations are bound to the variable name and not the base type, this line:
char *week,dayName; char *week, *dayName; |
|
|
|
|
|
#9 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Great, makes sense, and works wonderful.. Thanks..
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|