access dynamically allocated structure
-
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'
-
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'
Try this access in your loops :) :
{
CheckSumPair* psTempPair = (CheckSumPair*) ((BYTE*)CSPair + sizeof(CheckSumPair) * x);
// psTempPair->Member1
// psTempPair->Member2
}virtual void BeHappy() = 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'
-
Try this access in your loops :) :
{
CheckSumPair* psTempPair = (CheckSumPair*) ((BYTE*)CSPair + sizeof(CheckSumPair) * x);
// psTempPair->Member1
// psTempPair->Member2
}virtual void BeHappy() = 0;
Eugen Podsypalnikov wrote:
CheckSumPair* psTempPair = (CheckSumPair*) ((BYTE*)CSPair + sizeof(CheckSumPair) * x);
All this casting is unnecessary and prone to error, the compiler handles pointer arithmetic automatically.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
Try this access in your loops :) :
{
CheckSumPair* psTempPair = (CheckSumPair*) ((BYTE*)CSPair + sizeof(CheckSumPair) * x);
// psTempPair->Member1
// psTempPair->Member2
}virtual void BeHappy() = 0;
thanks eugen for the reply that was a typing mistake issue is resolved by using CheckSumpair[x].memberN; . . . since i am using [] operator to access pointer no need of using ->
-
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'
I think you have to print the values as follows:
for(int x = 0; x < 10; x++)
printf("%d %s %s\n\n",CSPair[x].weakcs, CSPair[x].StrongCS, CSPair[x].StrongCSString);or
for(int x = 0; x < 10; x++)
{
printf("%d %s %s\n\n",CSPair->weakcs,CSPair->StrongCS,CSPair->StrongCSString);CSPair++;
} -
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'
Your code is really ugly.
rupeshkp728 wrote:
#define UINT32 unsigned int #define INT32 int #define UCHAR unsigned char
Why do you use
#define
instead oftypedef
?rupeshkp728 wrote:
typedef struct CheckSumPair { UINT32 weakcs; // The weak, rolling Adler32 checksum. UCHAR StrongCS[10]; };
Typedef what?
rupeshkp728 wrote:
CheckSumPair* CSPair = (CheckSumPair*)malloc(sizeof(CheckSumPair)*10);
Why are you using malloc? Do you really need to use
C
memory allocation functions?rupeshkp728 wrote:
strncpy((char*)CSPair->StrongCSString,(char*)"CEDVCD",10*2+1);
struct CheckSumPair
has noStrongCSString
member.rupeshkp728 wrote:
printf("%d %s %s\n\n",CSPair[x]->.weakcs,CSPair[x]->StrongCS,CSPair[x]->StrongCSString);
This is the ugliest part. What are you trying to do? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Eugen Podsypalnikov wrote:
CheckSumPair* psTempPair = (CheckSumPair*) ((BYTE*)CSPair + sizeof(CheckSumPair) * x);
All this casting is unnecessary and prone to error, the compiler handles pointer arithmetic automatically.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
In this case - it is necessary, since
sizeof(CheckSumPair*) != sizeof(CheckSumPair)
! :)virtual void BeHappy() = 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'
is CheckSumPair not CheckSumPair*, so you can't use "->", use "." instead. Instruction "CSPair[x]->.weakcs" contains "->" and ".". Why malloc? use the new operator instead:CheckSumPair* CSPair = new CheckSumPair[10];
or better, if possible:
CheckSumPair CSPair[10];
And finally, you have to explain what are you trying to do with the two strncpy, maybe your code has to be like:
#define UINT32 unsigned int
#define INT32 int
#define UCHAR unsigned chartypedef struct
{
UINT32 weakcs; // The weak, rolling Adler32 checksum.
UCHAR StrongCS[10 + 1]; // including the string terminator
UCHAR StrongCSString[10 + 1]; // including the string terminator
} CheckSumPair;int main()
{
CheckSumPair CSPair[10];for(int x = 0; x < 10; x++) { CSPair\[x\].weakcs = (UINT32)x+1; strncpy((char\*)CSPair\[x\].StrongCS, "XYZ", 10); strncpy((char\*)CSPair\[x\].StrongCSString, "CEDVCD", 10); } for(int x = 0; x < 10; x++) { printf("%d %s %s\\n\\n", CSPair\[x\].weakcs, CSPair\[x\].StrongCS, CSPair\[x\].StrongCSString); } return 0;
}
-
In this case - it is necessary, since
sizeof(CheckSumPair*) != sizeof(CheckSumPair)
! :)virtual void BeHappy() = 0;
-
Pointers to structures can be incremented by simple expressions thus:
CSPair++;
that is all that is needed to point to the next entry in the array.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
Yes, they can !!! :-D You are right, Richard ! (probably I was intricated by
malloc
... :-D ) Thank you very much ! :thumbsup:virtual void BeHappy() = 0;