简体   繁体   English

下标值的错误既不是数组也不是指针也不是向量

[英]Error on subscripted value is neither array nor pointer nor vector

I read other answer too related to this error but my question is not that how to solve it. 我读了其他与此错误也很相关的答案,但我的问题不是如何解决它。 Rather it is why there is the difference in below two snippets 而是这就是为什么下面两个片段之间存在差异

void sortArray(int a[],int n,int x){
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int a = abs(x-a[j]);
      int b = abs(x-a[j+1]);
      if(a > b)
        swap(&a[j],&a[j+1]);
   }
  }
}

The other snippets does generates the error. 其他片段确实会产生错误。

void sortArray(int a[],int n,int x){
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      if(abs(x-a[j]) > abs(x-a[j+1]))
        swap(&a[j],&a[j+1]);
   }
  }
}

While the above one does not. 虽然上面的没有。 I wanted to know the internal working. 我想知道内部工作原理。

Error is on line no: 4,5 and 7 in first code: Subscripted value is neither array nor pointer nor vector 错误出现在第一个代码的第4、5和7行:下标值既不是数组,也不是指针,也不是向量

The crux of the matter is the variable names. 问题的症结在于变量名。 Allow me to remove everything that is irrelevant from the first function: 请允许我从第一个功能中删除所有不相关的内容:

void function(int a[]){
      int a = /* whatever */;
      a[0];
}

You have two things referred to by the same identifier ( a ). 您使用相同的标识符( a )引用了两件事。 In both C and C++ the rules dictate that a later declaration will hide the previous declaration. 在C和C ++中,规则都规定以后的声明将隐藏前一个声明。 So the integer hides the pointer from the moment it is declared (recall that "arrays" are adjusted to pointers in function parameter lists), and you lose the ability to refer to the pointer parameter. 因此,整数从声明它的那一刻起就隐藏了指针(记得“数组”已调整为函数参数列表中的指针),并且您失去了引用指针参数的能力。

That's why a[i] is an invalid expression, because the a it attempts to subscript is no longer an array or pointer. 这就是a[i]是无效表达式的原因,因为它尝试下标的a不再是数组或指针。

The error is here: 错误在这里:

 int a = abs(x-a[j]);

Prior to this line, a is defined with type int [] . 在此行之前,使用类型int []定义a But at this line, you redefine a as an int which masks the prior definition. 但是在这一行,您将a重新定义为可掩盖先前定义的int As a result the expression a[j] is referring to the newly declared a which is not a pointer or an array. 结果,表达式a[j]引用的是新声明的a ,它不是指针或数组。

Change the name of this variable to something else: 将此变量的名称更改为其他名称:

  int c = abs(x-a[j]);
  int b = abs(x-a[j+1]);
  if(c > b)
    swap(&a[j],&a[j+1]);

声明:本站的技术帖子网页,遵循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: 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 下标值既不是数组也不是指针,也不是数组索引处的向量 - Subscripted value is neither array nor pointer nor vector at array index 下标的值既不是数组,也不是指针,也不是向量 - subscripted value is neither array nor pointer nor vector in c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM