srl100 - 2022-10-28

Cppcheck version 2.9

Hi all.

I'm having trouble with what looks like a false positive when initialising an array of pointers to structs.

In the example below I have two structures being initialised, one with a single pointer to a struct (which passes the checks OK) and one with an array of two pointers to structs (which fails the checks):

Source\test.c:7:9: unusedStructMember : struct member 'statusStruct::One' is never used.
Source\test.c:8:9: unusedStructMember : struct member 'statusStruct::Two' is never used.
Source\test.c:16:19: unusedStructMember : struct member 'testDataStruct1::TestStatusElement' is never used.
Source\test.c:27:19: unusedStructMember : struct member 'testDataStruct2::TestStatusTable' is never used.
Source\test.c:30:24: misra-c2012-9.2 : The initializer for an aggregate or union shall be enclosed in braces
Source\test.c:30:24: misra-c2012-9.3 : Arrays shall not be partially initialized

[The 'is never used' warnings are side effects of extracting this code into a small example!]

test.c:

#define TOTAL_STATUS_TABLE  10

typedef struct
{
    int One;
    int Two;
} statusStruct;

static statusStruct StatusArray[TOTAL_STATUS_TABLE];

/* This example works OK */
typedef struct
{
    statusStruct* TestStatusElement;
} testDataStruct1;

static testDataStruct1 testDataTable1 =
{
     &StatusArray[0];
};

/* This examples warns that the initializer should be enclosed in braces, and that the array is partially initialized */
typedef struct
{
    statusStruct* TestStatusTable[2];
} testDataStruct2;

static testDataStruct2 testDataTable2 =
{
    {
        &StatusArray[0],    /* First element in table */
        0                   /* Second element in table */
    }
};
 

Last edit: srl100 2022-10-28