我想在一个数组中包含字符和整数。我想做的是在我的数组中有 1 到 9,然后用户选择要用字母 X 替换的数字。我该怎么做?我假设我无法将字符传递到称为 int array[8];
的数组中,那么有没有办法在数组中同时包含 int 和 char?
最佳答案
在 C++ 中,int
和 char
是几乎相同的东西。它们都存储为数字,只是分辨率不同。
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/