I want to fill a structure and then print it.
How to print the values of the structue?
The structure is:

#define UINT32 unsigned int
#define INT32 int
#define UCHAR unsigned char

typedef struct CheckSumPair
{
    UINT32 weakcs;                                   // The weak, rolling Adler32 checksum.
    UCHAR StrongCS[10];
};

I have dynamically allocated memory to it as folows.

CheckSumPair* CSPair = (CheckSumPair*)malloc(sizeof(CheckSumPair)*10);

for(int x = 0;x < 10;x++)
{
        CSPair->weakcs = (UINT32)x+1;
        strncpy((char*)CSPair->StrongCS,(char*)"XYZ",10);
        strncpy((char*)CSPair->StrongCSString,(char*)"CEDVCD",10*2+1);
}

Now print the values

for(int x = 0;x < 10;x++)
    printf("%d    %s  %s\n\n",CSPair[x]->.weakcs,CSPair[x]->StrongCS,CSPair[x]->StrongCSString);

While printing the values I get the following error:

error C2232: '->CheckSumPair::StrongCS' : left operand has 'struct' type, use '.'
error C2819: type 'CheckSumPair' does not have an overloaded member 'operator ->'
see declaration of 'CheckSumPair'

How to resove the issue?

Recommended Answers

All 3 Replies

I want to fill a structure and then print it.
How to print the values of the structue?
The structure is:

#define UINT32 unsigned int
#define INT32 int
#define UCHAR unsigned char

typedef struct CheckSumPair
{
    UINT32 weakcs;                                   // The weak, rolling Adler32 checksum.
    UCHAR StrongCS[10];
};

I have dynamically allocated memory to it as folows.

CheckSumPair* CSPair = (CheckSumPair*)malloc(sizeof(CheckSumPair)*10);

for(int x = 0;x < 10;x++)
{
        CSPair->weakcs = (UINT32)x+1;
        strncpy((char*)CSPair->StrongCS,(char*)"XYZ",10);
        strncpy((char*)CSPair->StrongCSString,(char*)"CEDVCD",10*2+1);
}

Now print the values

for(int x = 0;x < 10;x++)
    printf("%d    %s  %s\n\n",CSPair[x]->.weakcs,CSPair[x]->StrongCS,CSPair[x]->StrongCSString);

While printing the values I get the following error:

error C2232: '->CheckSumPair::StrongCS' : left operand has 'struct' type, use '.'
error C2819: type 'CheckSumPair' does not have an overloaded member 'operator ->'
see declaration of 'CheckSumPair'

How to resove the issue?

Your checksum pair structure does not contain a member StrongCSString for one. I'm assuming that this is a typo...

When accessing your pointer using the subscript operator this also has the effect of dereferencing it too so you do not need to use the -> operator but the . operator instead.

eg.

CheckSumpair[x].blah;

thanks mattjbond.
yes it was a typring mistake.
Thanks for the important input which solved the issue.

This is C++, you should be using new not malloc. This will allow you to remove that nasty cast. Casts should be avoid if possible, basically a cast (especially a C style cast) short circuits all your compilers type checking which is bad.

In you loop CSPair-> always references the first item in the array. Basically you put data into the first item in the array 10 times but never put anything into items 1 - 9.

The magic number 10 appears all over you code. Avoid magic numbers, define a const somewhere and use that.

Personally I would typedef rather than #define my integer types.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.