我正在尝试按值将动态分配数组 stillPlaying
的内容复制到 tmpIntArray
,只要该值不是 -1
,即 tmpIntArray
应该包含 stillPlaying
中的所有值,没有 -1
。
我在尝试编译时收到错误消息 subscripted value is neither array nor pointer nor vector
。
tmpIntArray = (int*) malloc(nParticipantsLeft*sizeof(int));
for(i = 0; i < nParticipantsLeft; i++)
{
if (stillPlaying[k] == -1)
k++;
tmpIntArray[i] = stillPlaying[k];
}
感谢您帮我解决这个看似简单的问题。
文件顶部的声明:
int * stillPlaying, //points to array of IDs for players who are not out
tmpIntArray; //holds the intermediate version of stillPlaying
最佳答案
将声明更改为
int *stillPlaying,
*tmpIntArray; //Notice *
根据您之前的声明,stillPlaying
是 pointer
但 temIntArray
是 int
(不是 pointer
)
此外,确保为 tmpIntArray
分配内存
关于在 C 中按值将一维数组的内容复制到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22291929/