gpt4 book ai didi

C、字符数组,错误: subscripted value is neither array nor pointer nor vector?

转载 作者:行者123 更新时间:2023-11-30 20:14:44 26 4
gpt4 key购买 nike

我是编写代码的新手,所以请简要解释您的答案,以便我可以(尝试)跟上,谢谢!我正在尝试输入一个字符串,将该字符串分配给一个字符数组,并有选择地提取所述字符字符串的一部分以返回(即输入“字符”并返回“行为”),我似乎无法理解为什么我不断收到错误“下标值既不是数组也不是指针也不是 vector ”。这是我的代码:

 #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
^"

最佳答案

您应该为 substring() 的参数声明显式类型:

char substring(char* source, int start, int count, char* result)

关于C、字符数组,错误: subscripted value is neither array nor pointer nor vector?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24896012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com