尝试将现有数组的反向存储到另一个数组中时C++错误。无法使用指针

C++ Error when trying to store the reverse of an already existing array into another array. Can't use pointers

本文关键字:数组 错误 C++ 指针 存储 另一个      更新时间:2023-10-16
//Function to store the reverse of original array   
void ReverseName(char name[], char reverse[]) {
int i, j = 0;
for (i = 0; name[i] != ''; i++) {} //loop to find size of name
for (i; i >= 0; i--) {              //loop to go backwards from end of name    
reverse[j] = name[i];           //should get stored here
j++;                       //run through array and populate it
}
}

似乎代码没有正确复制并且输出为空白。当我放一个"cout"来检查 name[] 时,它会检测并反向打印它没有问题,问题是复制到另一个数组中。

问题是你用i''的索引开始第二个循环,所以反转的字符串在第一个字符中会有'',因此看起来是空的。

替换此内容:

for (i; i >= 0; i--)

跟:

while (i--)

第二个循环完成后,终止字符串:

reverse[j] = '';