简体   繁体   中英

array of char assignment does not work

I am trying to change the value of my char array[10] within a switch statement such that, if we have case 1, my char array[10]="january", or if we have case 2, then array[10]="february" and so on. The problem is that i get error message, and i know that I am doing something wrong. any help will be very appreciated. here is my switch statement written in Dev-C++.

char month[10];
switch (i)
{
 case 1:
       month[10]="January";
      cout<<month<<endl;
      break; 
 case 2:
       month[10]="February";
      cout<<month<<endl;
      break;
} 

You can't assign char arrays like that, you need to use strcpy .

Or better yet, use std::string , it's the C++ thing to do.

std::string month;
switch (i)
{
 case 1:
       month="January";
      cout<<month<<endl;
      break; 
 case 2:
       month="February";
      cout<<month<<endl;
      break;
} 

Or if you must stick to char[] :

char month[10];
switch (i)
{
 case 1:
      strcpy(month,"January");
      cout<<month<<endl;
      break; 
 case 2:
      strcpy(month,"February");
      cout<<month<<endl;
      break;
} 

The reason you're getting the error is that month[10] is a char , and you're trying to assign a const char* to it, which is illegal. (actually it's undefined behavior, because 10 is beyond the length of the array).

For a quick fix, replace:

month[10] = "January";

with:

strcpy (month, "January");

or an equivalent like strncpy , if you're one of those types who can't figure out how big their buffers should be - "September", the longest month, will quite happily fit in ten bytes :-)

What the former piece of code is doing is trying to set the character at offset 10 of your array to that string, clearly not viable because:

  1. You can't fit 8 bytes into a 1-byte bucket.
  2. The offset 10 is beyond the end of the array anyway.

Of course, an alternative that doesn't use up so many lines in your code could be something like:

static char *months[] = { "January", "February", ..., "December" };
if ((i < 1) or (i > 12))
    strcpy (month, "?");
else
    strcpy (month, months[i-1]);
cout << month << endl;

For a proper fix, stop using C-style strings in your C++ code. The designers of C++ put a huge amount of effort providing std::string just so you wouldn't experience problems like this.

You can't assign a string literal to an array. You'll have to use strcpy() instead.

An alternative way would be to use std::string instead of char array.

When you create strings like "January" or "December" the compiler is generating a const char * which points to a constant array of characters that has a 0 at the end the integer not the character .

Your code is trying to assign "January" 's address to the 11th element in the array, which it doesn't have because arrays start at 0.

This has a couple problems, First, the elements of month[] are characters, not character pointers. Second, arrays start at 0 so you were looking to use month[9] , though that still would have been wrong.

What you are looking to do is copy each character into the array, there are functions that do this like strcpy but try it with a for loop first to give yourself a better sense of what is going on.

Another way to solve this problem, and probably the way you're after is to change month from a character array of a fixed size, to a char * that way you can use your earlier method of month = "January" its how I would prefer to do it myself, because it only uses up as much memory as I need and the syntax is cleaner, though you should know how to do both methods.

Though it is more a stylistic note, your usage of switch can be vasly improved if you use it in specialized functions:

// Using C-string
char const* selectMonth(int i) {
  switch(i) {
  case 1: return "January";
  case 2: return "February";
  ...
  }
  assert(0 && "This is impossible!");
}

Then you can use this quite easily:

char month[10];
strcpy(month, selectMonth(i));

In general, you should find that using small functions help making code more readable. And in true C++ style, you even get const-ness goodness:

std::string const month = selectMonth(i); // note: the very same selectMonth!
            ~~~~~

This const will ensure that you do not accidentally modify your variable afterwards, it only works if you can initialize it immediately, which requires a small function to do the job. Goodness begets goodness :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM