简体   繁体   English

错误:下标值既不是数组也不是指针

[英]error: subscripted value is neither array nor pointer

int get_first(int arr[],int count)
{

   int half = count / 2;

   int *firstHalf = malloc(half * sizeof(int));
   memcpy(firstHalf, arr, half * sizeof(int));
   return firstHalf;
}

int get_second(int arr[], int count)
{
    int half = count / 2;

    int *secondHalf = malloc(half * sizeof(int));
    memcpy(secondHalf, arr + half , half * sizeof(int));
    return secondHalf;
}

int result = get_first(arr, count);
int size = sizeof(result) / sizeof(result[0]);

i am writing a function that split an array into two equal parts. 我正在编写一个将数组拆分为两个相等部分的函数。 the function takes in an array and the size of the array. 该函数接受一个数组和数组的大小。 I am testing the function by storing the first half of the array in the result and print its length. 我通过将数组的前半部分存储在结果中并打印其长度来测试该功能。 But when I build the function, the line 但是当我构建函数时,

int size = sizeof(result) / sizeof(result[0]);

gives an error says "error: subscripted value is neither array nor pointer" 给出错误提示“错误:下标值既不是数组也不是指针”

Is it because my function failed to pass the first half of the array into result? 是因为我的函数无法将数组的前半部分传递给结果吗? or the way of storing an array is wrong? 还是存储数组的方式错误? If so, how do I split the array, can someone help me to fix it? 如果是这样,我如何拆分阵列,有人可以帮助我修复它吗? thanks in advance. 提前致谢。

There are two problems that I can see: 我可以看到两个问题:

  1. In functions int get_first(int arr[],int count) and int get_second(int arr[], int count) you are returning int pointers but functions' return type are int. 在函数int get_first(int arr[],int count)int get_second(int arr[], int count)您将返回int指针,但函数的返回类型为int。
  2. result is declared as int but you are accessing it like result[0]. result被声明为int,但是您像result [0]一样访问它。

Correction for 1 is obvious from point 1 above. 从上面的点1可以明显看出对1的校正。 Correction for 2: 更正2:

Instead of: 代替:

int result = get_first(arr, count);

You should write: 您应该写:

int *result = get_first(arr, count);

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “错误:下标值既不是数组,也不是指针,也不是矢量” - “error: subscripted value is neither array nor pointer nor vector” 持续错误:下标值既不是数组也不是指针也不是向量 - persistent error: subscripted value is neither array nor pointer nor vector 错误:C中的下标值既不是数组也不是指针也不是矢量 - error: subscripted value is neither array nor pointer nor vector in C 错误-下标值既不是数组也不是指针也不是向量 - error - subscripted value is neither array nor pointer nor vector 下标值既不是数组也不是指针也不是向量的错误 - Error with subscripted value being neither array nor pointer nor vector 下标值的错误既不是数组也不是指针也不是向量 - Error on subscripted value is neither array nor pointer nor vector “错误:下标值既不是数组,也不是指针,也不是矢量”,但是为什么呢? - “error: subscripted value is neither array nor pointer nor vector”, but why? 错误:下标值既不是数组也不是指针也不是向量 - Error: subscripted value is neither array nor pointer nor vector 错误 - C 中的下标值既不是数组也不是指针也不是向量 - Error - Subscripted value is neither array nor pointer nor vector in C C - 错误:下标值既不是数组也不是指针 - C - error: subscripted value is neither array nor pointer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM