Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have just started learning C++ and for my first assignment i'm using a function of a switch statement which includes strings... i read somewhere on the internet that this can create a bug?!?!?
i just want to be able to use my program.
I'm using dev- c++ bloodshed
char func(int month)
{char output_month[4];
switch(month)
{
case 1:
strcpy(output_month,"Jan");
break;
case 2:
strcpy(output_month,"Feb");
break;
......etc....default:
/* Do nothing */;
}
return output_month[4];
}i recall it by using
cout << func(month1)
it compiles ok, but doesn't display the string of characters.. only weird symbols?!?!
anyone that could suggest a way to beat it, or a SIMPLE alternative would receive my greatest thanx! Kim

You have four problems.
First two problems are in the line:
return output_month[4];
1-You declared a 4 element array, but are telling it to access the 5th element of the array. Remember that the first element is at output_month[0]. So you are accessing memory that is outside the array (C & C++ will let you do that).
2-By putting the [ ] on output_month, you are telling it to return only one character, the character at array position 4 (in your code). You want to return the whole string, string, so don't tell it to return a single character, but tell it to return the whole string by ...
3-Since you want to return the whole string (which is a char*) rather than a single character, you want to change your function definition to return a char* rather than a char. "char func(blah)" should be "char *func(blah)"
4-The final problem and the trickiest. Put the word "static" before your definition of output_month.
static char output_month[4];
There is a good chance that your program will appear to run fine without this, but that would just be luck. This explanation may be a little advanced for the level you are at if you just started.
But by doing "return output_month", you are telling it to return the address in memory of the array output_month. So the cout function uses that address to get a string to output.
No problem. The problem is that output_month is a local variable and local variables disappear after the function exits. The function "func" will exit before the cout executes, so the variable output_month doesn't exist when the cout is printing the string. So it is possible that you could get garbage. Adding "static" changes how the array is stored, so that it won't disappear when the function exits.
But, there is a good chance that you won't have this problem, because the program is simple enough that the memory where the output_month array was stored probably won't get overwritten. Even tho the variable doesn't exist anymore, if the memory that it used to use doesn't get reused, then you won't notice a problem.
But things like this are things that you'll need to learn to know about and notice if you program in C/C++.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |