【发布时间】:2014-07-22 19:23:46
【问题描述】:
我是编写代码的新手,所以请简要解释您的答案,以便我(尝试)跟上,谢谢! 我正在尝试输入一个字符串,将该字符串分配给一个字符数组,并有选择地提取一部分所述字符字符串以返回(即输入“字符”并返回“动作”),我似乎无法理解为什么我不断收到错误“下标值既不是数组也不是指针也不是向量”。这是我的代码:
#include <stdio.h>
char source[81], result[81];
int start, count;
char substring ();
int main(void)
{
substring ("character", 4, 3, result);
printf ("%s", result); //print result
return 0;
}
char substring (source, start, count, result)
{
int i = 0;
while (i <= (count-1))
{
result[i] = source[((start-1)+i)]; //op chosen chars to result array
++i;
if (i == count)
{
result[i] = '\n'; //when i = count, insert null at end of source array
}
}
return result;
}
当我尝试编译时,我得到了错误: "
Compilation error time: 0 memory: 0 signal:0
prog.c: In function ‘substring’:
prog.c:20:13: error: subscripted value is neither array nor pointer nor vector
result[i] = source[((start-1)+i)]; //op chosen chars to result array
^
prog.c:20:25: error: subscripted value is neither array nor pointer nor vector
result[i] = source[((start-1)+i)]; //op chosen chars to result array
^
prog.c:24:14: error: subscripted value is neither array nor pointer nor vector
result[i] = '\n'; //when i = count, insert null at end of source array
^"
【问题讨论】:
-
是的,我知道要提取“act”,我需要将“4”参数更改为“5”或修复数组子集选择条件
-
您的函数需要四个
int参数。如果要使用全局变量,只需使用它们而不是为它们设置参数。一个更好的想法是摆脱全局变量并更改参数以具有正确的类型,以便您可以传递数据。 -
函数
char substring (source, start, count, result)应该类似于char* substring (char *source,int start,int count,char* result) -
您拥有的不是有效的 C 代码。
-
忽略消息的“或向量”部分。在这种情况下,“向量”不是 C++
std::vector;它指的是特定于 gcc 的语言扩展,documented here。
标签: c arrays pointers compiler-errors