[英]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. 我正在尝试在switch语句中更改我的char数组[10]的值,这样,如果我们有情况1,我的char数组[10] =“ january”,或者如果我们有情况2,则array [10] =“ february”,依此类推。 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++.
这是我用Dev-C ++编写的switch语句。
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
. 您不能像这样分配
char
数组,需要使用strcpy
。
Or better yet, use std::string
, it's the C++ thing to do. 或者更好的方法是使用
std::string
,这是C ++要做的事情。
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[]
:
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. 出现错误的原因是
month[10]
是一个char
,并且您试图为其分配一个const char*
,这是非法的。 (actually it's undefined behavior, because 10 is beyond the length of the array). (实际上,这是未定义的行为,因为10超出了数组的长度)。
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 :-) 或类似
strncpy
的等效项,如果您是无法弄清其缓冲区应该多大的类型之一-最长的月份“ September”将很高兴地容纳十个字节:-)
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: 前一段代码正在做的是尝试将数组偏移量10处的字符设置为该字符串,显然不可行,因为:
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. 为了获得正确的解决方案,请停止在C ++代码中使用C样式的字符串。 The designers of C++ put a huge amount of effort providing
std::string
just so you wouldn't experience problems like this. C ++的设计人员投入了大量精力来提供
std::string
,这样您就不会遇到这样的问题。
You can't assign a string literal to an array. 您不能将字符串文字分配给数组。 You'll have to use
strcpy()
instead. 您必须改为使用
strcpy()
。
An alternative way would be to use std::string
instead of char
array. 另一种方法是使用
std::string
代替char
数组。
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 . 当您创建
"January"
或"December"
之类的字符串时,编译器将生成一个const char *
,该const char *
指向一个常量字符数组,该数组的末尾为0
,而不是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. 您的代码正在尝试将
"January"
的地址分配给数组中的第11个元素,因为数组从0开始,所以它没有此地址。
This has a couple problems, First, the elements of month[]
are characters, not character pointers. 这有两个问题,首先,
month[]
的元素是字符,而不是字符指针。 Second, arrays start at 0 so you were looking to use month[9]
, though that still would have been wrong. 其次,数组从0开始,因此您希望使用
month[9]
,尽管那仍然是错误的。
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. 您要执行的操作是将每个字符复制到数组中,有些函数可以执行此操作,例如
strcpy
但请先使用for
循环进行尝试,以使自己对正在发生的事情有更好的了解。
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. 解决此问题的另一种方法(可能是您要遵循的方法)是将
month
从固定大小的字符数组更改为char *
,这样您便可以使用更早的month = "January"
方法了,我更喜欢自己做,因为它只使用我需要的内存,并且语法更简洁,尽管您应该知道如何使用这两种方法。
Though it is more a stylistic note, your usage of switch can be vasly improved if you use it in specialized functions: 尽管它更像是一种风格上的注释,但是如果您在专用功能中使用它,可以大大改善switch的用法:
// 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:
在真正的C ++风格中,您甚至可以获得const-ness的好处:
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. 此
const
可以确保您以后不会意外修改变量,只有在可以立即对其进行初始化的情况下它才起作用,这需要一个小的函数来完成。 Goodness begets goodness :) 善良会带来善良:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.