简体   繁体   English

指向结构的指针成员

[英]Pointing struct's pointer member

I am getting an error from my compiler as follow: 我从编译器收到如下错误:

C51 COMPILER V9.01 - SN: C1ADC-HAI60D COPYRIGHT KEIL ELEKTRONIK GmbH 1987 - 2009 * WARNING C260 IN LINE 300 OF SEQUENCE.C: '=': pointer truncation * ERROR C190 IN LINE 301 OF SEQUENCE.C: '&': not an lvalue C51编译器V9.01-SN:C1ADC-HAI60D版权所有KEIL ELEKTRONIK GmbH 1987-2009 *警告SEQUENCE.C第300行中的C260:'=':指针截断* SEQUENCE.C 301行中的错误C190:'&':不是左值

The following is my code: 以下是我的代码:

   struct myCond{
    unsigned char currStatus;
    unsigned char prevStatus;
    unsigned int *timer;
    unsigned char *flag;
   }
  struct myCond StatCond;

  unsigned int data timerdata;
  bit bdata timeflag;

  void someSubroutine (void)
  {
    struct myCond *tempCond;

tempCond = &StatCond;
tempCond->timer = &((unsigned int)timerdata);
tempCond->flag = &((unsigned char)timeflag);
  }

Are we supposed to guess which line is 301? 我们是否应该猜测哪条线是301?

The problems, as I understand are here: 据我了解,问题出在这里:

tempCond->timer = &((unsigned int)timerdata);
tempCond->flag = &((unsigned char)timeflag);

(unsigned int)timerdata and (unsigned char)timeflag are values, r-values to be precise. (unsigned int)timerdata(unsigned char)timeflag是值, (unsigned char)timeflagr-values They cannot be modified or assigned unlike l-values , which plain timerdata and timeflag are. l-values不同,不能对其进行修改或分配,而普通timerdatatimeflagl-values And so you can't take addresses of r-values with & . 因此,您不能使用&接受r-values地址。 It would be the same as, say, writing &1 . 就像写&1 1 on its own does not exist as an object in data memory. 1本身不作为对象存在于数据存储器中。

You should write instead: 您应该改写:

tempCond->timer = &timerdata;
tempCond->flag = (unsigned char*)&timeflag;

And I'm not quite sure that it is legal to take an address of a bit variable. 而且我不太确定采用位变量的地址是否合法。 The last line may fail to compile. 最后一行可能无法编译。

Perhaps redefining the structure would help: 也许重新定义结构会有所帮助:

struct myCond{
...
    bit bdata *flag; // or maybe without bdata
   }

and then you'd write tempCond->flag = &timeflag; 然后编写tempCond->flag = &timeflag; .

unsigned int data timerdata;  // what is 'data', is it defined?
bit bdata timerflag;  // what are 'bit' and 'bdata', are they defined?

Check your code with regard to my questions above. 关于上述问题,请检查您的代码。 Compiler errors are often reported multiple lines after the real offense. 真正的攻击后,编译器错误通常报告为多行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM