递增数组变量时左值不一致所需错误 [英] Inconsistent lvalue required error when incrementing array variable

查看:108
本文介绍了递增数组变量时左值不一致所需错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到要求作为增量的操作数左值,而这样做 * ++中的。我要去的地方错了吗?我认为这将等同于 *(A + 1)。这种行为是不可思议的 * argv的++ 工作正常。请帮助。

 的#include<&stdio.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
  的printf(精氨酸为:=%S \\ n,* argv的++);  INT A1 [] = {1,2,3,4,5,6};
  INT A2 [] = {7,8,9,10,11,12};
  为int * a [2];  一个[0] = A1;
  一个[1] = A2;  的printf(PTR =%d个\\ N,* ++ a)条;  返回0;
}


解决方案

A 是一个常量(数组名),你不能做改变其值 ++中的,也就是等于 A = A + 1 。结果
你可能会想要做的 * A = * A + 1 (增量0索引值),这个尝试 *一个++ 代替。注意我已经改变了 ++ 从preFIX为后缀。

请注意:的char * argv的[] 被定义为功能参数和的argv 是指针的变量字符的EM> ** 键入(的char * [] )。结果
而在code 为int * A [] 不是一个函数的参数。在这里, A 为int类型的数组(为int * )(数组名称不变)。记得在函数的参数声明和正常的声明是不同的。

此外,通过使用 ++ 的argv A 你刚刚发现一个区别,多了一个有趣的差异,如果你使用的sizeof 运营商那里打印尺寸可以观察。例如检查这个工作code codepade

  INT主(INT ARGC,CHAR *的argv []){
    为int * a [2];
    的printf(A:%U,argv的:%U的sizeof(A)的sizeof(ARGV));
    返回1;
}

输出是:

  A:8,argv的:4

在系统地址大小为4个字节。输出 8 是数组的大小 A 由int类型地址的两个元素(因为 A 是数组),而 4 的argv 的大小(因为的argv 是指针)。

I am getting "lvalue required as increment operand" while doing *++a. Where I am going wrong? I thought it will be equivalent to *(a+1). This behaviour is weird as *++argv is working fine. Please help.

#include <stdio.h>

int main(int argc, char *argv[])
{    
  printf("Arg is:  = %s\n", *++argv);

  int a1[] = {1,2,3,4,5,6};    
  int a2[] = {7,8,9,10,11,12};
  int *a[2];

  a[0] = a1;
  a[1] = a2;

  printf("ptr  = %d\n", *++a);

  return 0;
}

解决方案

a is a constant(array name) you can't change its value by doing ++a, that is equal to a = a + 1.
You might wanted to do *a = *a + 1 (increment 0 indexed value), for this try *a++ instead. notice I have changed ++ from prefix to postfix.

Note: char* argv[] is defined as function parameter and argv is pointer variable of char** type ( char*[]).
Whereas in your code int* a[] is not a function parameter. Here a is an array of int type (int*)(array names are constant). Remember declaration in function parameter and normal declarations are different.

Further, by using ++ with argv and a you just found one difference, one more interesting difference you can observe if you use sizeof operator to print there size. for example check this working code Codepade

int main(int argc, char* argv[]){
    int* a[2];
    printf(" a: %u, argv: %u", sizeof(a), sizeof(argv));
    return 1;
}

Output is:

a: 8, argv: 4

Address size in system is four bytes. output 8 is size of array a consists of two elements of type int addresses (because a is array), whereas 4 is size of argv (because argv is pointer).

这篇关于递增数组变量时左值不一致所需错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆