1,105,625 Community Members

access dynamically allocated structure

Member Avatar
rkp728
Newbie Poster
20 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
mattjbond
Junior Poster
149 posts since Mar 2010
Reputation Points: 54 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
0
 

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;

Member Avatar
rkp728
Newbie Poster
20 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 4 Years Ago by mattjbond
Member Avatar
Banfa
Practically a Posting Shark
801 posts since Mar 2010
Reputation Points: 582 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 12 [?]
Featured
 
0
 

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.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article