如何在 char[ ] 数组中手动输入字符
How do I manually enter characters in a char[ ] array?
假设我声明了一个数组
字符名称[20];
并且我必须在变量中手动输入名称"Andrew"(少于 20 个字符),这怎么可能?
**编辑:我尝试了此代码,但它给了我此错误
错误:将常量字符 [10] 分配给字符 [30] 时的类型不兼容
#include<stdio.h>
#include<iostream.h>
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
student record1;
student record2;
record1.id=1;
record1.name = "Sir Simon";
record2.id=2;
record2.name = "de Canterville";
puts(record1.name);
puts(record2.name);
}
方法可以做到这一点,因为使用数组,您可以通过[x]
访问单个数组元素(其中x
是您希望访问的元素)。
在这些示例中,我假设"Andrew"是一个 C 字符串(即它以空字符结尾)。
第一种方法(最乏味的方法)是单独设置每个数组元素:
name[0] = 'A';
name[1] = 'n';
name[2] = 'd';
name[3] = 'r';
name[4] = 'e';
name[5] = 'w';
name[6] = 0; /* If this is not a C string, and you literally want the characters "Andrew" (and only these characters), you can skip this line) */
但是,这显然有点痛苦。值得庆幸的是,C 有一个简洁的功能,您可以在一行中初始化数组,而无需为每个元素提供单独的语句:
char name[20] = {'A', 'n', 'd', 'r', 'e', 'w', 0};
但也有更简单的方法!如果你使用双引号("..."
),它基本上会等同于上面的代码(用花哨的{...}
表示法):
char name[20] = "Andrew"; // This adds the NULL (0) character at the end
但是,我介绍的最后两个方法(虽然不是第一个,但第一个可以随时运行)只能在变量初始化期间运行,以后不能运行。换句话说,您不能这样做:
char name[20];
name = "Andrew";
在这种情况下,我们可以使用第一种方法(非常乏味!!),或者我们可以使用包含在 <string.h>
中的整洁函数,称为 strcpy
:
strcpy(name, "Andrew");
strcpy
的工作原理是将源("Andrew"
)复制到目标(name
),直到它满足空字符(并且也将复制空字符)。当您没有常量值时,此函数也很有用。例如,您可以执行以下操作:
char* my_home[20];
char* home_directory = getenv("HOME"); /* an example of a non-constant value */
strcpy(my_home, home_directory);
但是,如果要复制的字符串的长度大于目标数组的大小,则这可能非常不安全。为此,请使用 strncpy
,它将复制,直到达到 NULL 字符或已复制您指定的最大字符数:
strncpy(name, "Andrew", 20);
请注意,如果源 ( "Andrew"
) 的长度超过 20,则不会添加 NULL 字符。因此,如果您想将其保留为 C 字符串,请尝试以下操作:
strncpy(name, "Andrew", 19);
name[19] = 0;
编辑:至于为什么不可能做char name[20]; name = "Andrew";
,我不知道有任何技术原因使它不可能做到这一点。我猜这只是 C 语法的一个限制。
我决定看看汇编中会发生什么,所以,从这个源代码:
#include <stdio.h>
int main(int argc, char** argv)
{
char text[] = "Hello World!";
puts(text);
return 0;
}
程序集输出为:
...
movabsq $8022916924116329800, %rax ; Sets %rax to "Hello Wo"
movq %rax, -16(%rbp) ; Moves %rax to text[0-7]
movl $560229490, -8(%rbp) ; Sets text[8-11] to "rld!"
movb $0, -4(%rbp) ; text[12] = 0;
...
从理论上讲,这可以在任何时间点完成,而不仅仅是在初始化时完成。所以,至于为什么C不允许这样做,我不知道。现在,您只需要满足于使用 strcpy
,或者,如果您希望遵循程序集的功能:
*((uint64_t*)text) = 8022916924116329800;
*(((uint32_t*)text) + 2) = 560229490;
text[12] = 0;
除非你想严重惹恼你的同事,否则不要这样做,:P我很确定大多数现代编译器应该优化strcpy
函数,如果它是一个常量(即 "Andrew"
而不是变量或函数,如 getenv("HOME")
)。
您指定 c++ 语言和 char 数组并要求手动输入值。答案是:
char name[20] = "Andrew";
如果您打算从控制台读取值,请使用以下命令:
cin.get(name, 20);
但最好使用 c++ 库:
#include <string>
#include <iostream>
std::string name;
std::cin >> name;
要么这样:
char name[20] = "Andrew";
或者,没有句法糖:
char name[20] = {'A','n','d','r','e','w',' '};
您也可以使用 strcpy(name, "Some Name")
展开第一个答案,尝试此代码,它将显示此赋值如何在数组上工作。
#include <iostream>
using namespace std;
int main() {
char name[20] = "Andrew";
for(int i = 0; i < 20; i++)
{
cout << i << ": " << name[i] << endl;
}
return 0;
}
"Andrew"中的字符被分配给名称,固定大小数组的其余元素为空。
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- 在C++中使用字符数组进行输入验证
- 防止在字符数组中输入双倍空格在 C++
- C++ 中字符数组的输入验证
- 在大小为 10 的 char 数组中输入超过 10 个字符的字符串并输出时,它会显示整个数组而不是前 10 个数组.为什
- 使用终止程序的指针在数组中输入字符串
- C++ 对象数组字符串输入在控制台上不起作用
- 如何在不知道尺寸的情况下在 2D 数组中输入
- 使用 for 循环在数组中输入用户
- 需要帮助传递数组和输入数组
- 如何返回数组中输入的值的数目
- 当用户在字符数组中输入整数值时捕获异常
- 如何使用数组获取输入,并将其存储在record.h类的主类中的变量中
- 访问C 类中功能指针数组的输入
- GSOAP动态数组作为输入参数
- 在 C++ 的无符号短数组中输入字符串
- 如何在 C++ 中的字符数组中输入空格
- 在数组中输入5个元素,检查验证,然后输出C++