简体   繁体   English

访问结构成员的元素以进行比较

[英]Accessing an element of a member of a structure to compare

Question: For the code below, I am trying to compare individual elements of the Logger->Name and not the whole array to the Base_PN variable. 问题:对于下面的代码,我试图将Logger-> Name的各个元素而不是整个数组与Base_PN变量进行比较。 Can someone make some suggestions of how I can changes so that I can achieve this. 有人可以对我如何进行更改提出一些建议,以便我可以实现这一目标。 Thanks. 谢谢。

The code below is for my the logger.h file where i declare a new type of 8 bit UINT8 array called partNum_t which is 10 bytes wide or has 10 elements. 下面的代码适用于我的logger.h文件,其中我声明了一种称为partNum_t的新型8位UINT8数组,该数组宽10个字节或具有10个元素。 Then I made it part of a structure or a member of a structure called logger. 然后,我将其作为结构的一部分或称为记录器的结构的成员。 I know whenever I call the structure member, actually I am actually passing a memory address for example logger->Name will have a memory address. 我知道每当我调用结构成员时,实际上我实际上是在传递一个内存地址,例如logger-> Name将具有一个内存地址。 Same with logger->qty. 与logger-> qty相同。

//logger.h
typedef UINT8 partNum_t[10];

typedef struct 
{
   partNum_t Name; 
   UINT16 qty; 
} Logger;

UINT8* connected(void); //Function Prototype

The code below is the logger.c file. 下面的代码是logger.c文件。

#include "logger.h"
//Please note that although the type for Logger->Name is partNum_t the PIC32           MPLAB compiler throws errors if the partNum_t* is used instead of UINT8* as it is on the next line.  
UINT8* Get_PN (void)
{
  return (Logger->Name); 
}

The next file is the uses the information. 下一个文件是使用信息。 I have called it DisplayInfo.c 我称它为DisplayInfo.c

#include "logger.h"
..
..
partNum_t Base_PN={'1','2','3','4','5','-','7','8','9','0'}; 
UINT8* RawValue_PN;
BOOL setFlag; 

void use_PN()
{....
...

RawValue_PN = Get_PN();
if ( RawValue_PN && !memcmp(RawValue_PN, Base_PN, 10))
{
    {
    setFlag=1;
    }
    else 
    {
    setFlag=0;
    } 
}

All of above works fine but I am only interested in comparing the last 4 digits '7','8','9','0' of the Base_PN individually. 上面的所有方法都可以正常工作,但是我只想比较Base_PN的后4位“ 7”,“ 8”,“ 9”,“ 0”。 partNum_t Base_PN={'1','2','3','4','5','-','7','8','9','0'}; partNum_t Base_PN = {'1','2','3','4','5','-','7','8','9','0'}; How can I achieve this. 我该如何实现。

I have tried to UINT8 Get_PN6 (void) { return (Logger->Name[6]); 我已经尝试过UINT8 Get_PN6(void){return(Logger-> Name [6]); } and in the DisplayInfo.c file }并在DisplayInfo.c文件中

.....
partNum_t Base_PN={'1','2','3','4','5','-','7','8','9','0'}; 
BOOL setFlag;
UINT8 RawValue_PN6, BL6_current;
RawValue_PN6 = Get_PN6();
if (BL6_current != RawValue_PN6) //So that code only runs when there is change.
{
  if (RawValue_PN6==3)
   {
    setFlag=1;
    }
    else 
    {
    setFlag=0;
    } 
 BL6_current = RawValue_PN6;
}

But I get a warning: comparison between pointer and integer for this line 但是我得到一个警告:此行的指针和整数之间的比较

if (BL6_current != RawValue_PN6)

I know whenever I call the structure member, actually I am actually passing a memory address for example logger->Name will have a memory address. 我知道每当我调用结构成员时,实际上我实际上是在传递一个内存地址,例如logger-> Name将具有一个内存地址。 Same with logger->qty. 与logger-> qty相同。

Your problem comes from a misconception here. 您的问题来自这里的误解。 It's not whenever , but (from ISO/IEC 9899:201x): 不是每时每刻 ,而是(来自ISO / IEC 9899:201x):

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type '' is converted to an expression with type ''pointer to type '' that points to the initial element of the array object and is not an lvalue. 除非它是sizeof运算符, _Alignof运算符或一元&运算符的操作数,或者是用于初始化数组的字符串文字,否则将类型为“ array of type ”的表达式转换为带有键入指向阵列对象的初始元素,是不是左值“”指针为类型 “”。

So, what you believed to know applies to arrays, but not to an lvalue that does not have array type , particularly not to logger->qty and Logger->Name[6] , which are converted to the value stored in the designated object . 因此,您认为所知道的内容适用于数组,但不适用于没有数组类型的左值 ,特别是不适用于logger->qtyLogger->Name[6] ,后者会转换为存储在指定对象中的值

I am only interested in comparing the last 4 digits '7','8','9','0' of the Base_PN individually. 我只想分别比较Base_PN的后4位数字“ 7”,“ 8”,“ 9”,“ 0”。 partNum_t Base_PN={'1','2','3','4','5','-','7','8','9','0'}; partNum_t Base_PN = {'1','2','3','4','5','-','7','8','9','0'}; How can I achieve this. 我该如何实现。

You can use 您可以使用

UINT8 *Get_PN6(void) { return &Logger->Name[6]; }

or 要么

UINT8 *Get_PN6(void) { return Logger->Name+6; }

(which are equivalent), or, as suggested by Dmitri, stay with RawValue_PN = Get_PN() and (等效),或者按照Dmitri的建议,保留RawValue_PN = Get_PN()

    memcmp(RawValue_PN+6, Base_PN+6, 4)

That would work but I need individual access to each byte so I can compare each one and and then display individual character. 可以,但是我需要分别访问每个字节,因此我可以比较每个字节,然后显示单个字符。

You do already have individual access to each byte through RawValue_PN[6] , RawValue_PN[7] , Base_PN[6] , etc. 您已经可以通过RawValue_PN[6]RawValue_PN[7]Base_PN[6]单独访问每个字节

I got rid of the Warning about comparison between pointer and integer by using typecasting 我通过使用类型转换摆脱了关于指针和整数之间比较的警告

This way is wrong. 这种方式是错误的。 By converting an UINT8 to an UINT8 * , you are reinterpreting a character value (eg '7') as a memory address, which is most probably fatal for your program and certainly not what you want. 通过将UINT8转换为UINT8 UINT8 * ,您正在将字符值(例如“ 7”)重新解释为内存地址,这很可能对您的程序致命,当然也不是您想要的。

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

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