newb7777
newb7777

Reputation: 563

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. 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. 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. Same with 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.

#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

#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. 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]); } and in the DisplayInfo.c file

.....
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)

Upvotes: 1

Views: 74

Answers (1)

Armali
Armali

Reputation: 19375

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. Same with logger->qty.

Your problem comes from a misconception here. It's not whenever, but (from 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.

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.

I am only interested in comparing the last 4 digits '7','8','9','0' of the Base_PN individually. 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

    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.

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 (e. g. '7') as a memory address, which is most probably fatal for your program and certainly not what you want.

Upvotes: 1

Related Questions