C,char数组,错误:下标值既不是数组也不是指针也不是向量?

时间:2014-07-22 19:23:46

标签: c arrays pointers compiler-errors

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

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

1 个答案:

答案 0 :(得分:2)

您应该为substring()

的参数声明显式类型
char substring(char* source, int start, int count, char* result)