c++ - 一个数组中的 Char 和 Int

标签 c++ c arrays char int

我想在一个数组中包含字符和整数。我想做的是在我的数组中有 1 到 9,然后用户选择要用字母 X 替换的数字。我该怎么做?我假设我无法将字符传递到称为 int array[8]; 的数组中,那么有没有办法在数组中同时包含 int 和 char?

最佳答案

在 C++ 中,intchar几乎相同的东西。它们都存储为数字,只是分辨率不同。

int array[2];
array[0] = 100;
array[1] = 'c';

printf("%d", array[0]) //Prints the number at index zero.

//Is it %c to print a char?
printf("%c", array[1]) //Prints the number at index zero as it's equivalent char.

关于c++ - 一个数组中的 Char 和 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8043359/

相关文章:

c++ - 隐藏 QPushButton 和 QLineEdit 边框

.net - 非 .Net C++ 代码的 FxCop(或等同物)

c++ - 创建只能在父窗口内拖动的窗口

c - 让 dyld 链接到可执行文件本身的函数?

php - 试图理解 mysqli,需要一个以一列作为键、一列作为值的数组

java - 如何检查数组的随机部分是否等于特定数组部分?

python - 如果满足条件,从数组中减去一个数字python

c++ - 在 C++ 中调用构造函数

c++ - 删除右 child 使父左指针指向 nullptr

c - 在 C 中从命令行参数打印子字符串