Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. access dynamically allocated structure

access dynamically allocated structure

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelptutorialquestion
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R rupeshkp728

    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'

    E Offline
    E Offline
    Eugen Podsypalnikov
    wrote on last edited by
    #2

    Try this access in your loops :) :

    {
    CheckSumPair* psTempPair = (CheckSumPair*) ((BYTE*)CSPair + sizeof(CheckSumPair) * x);
    // psTempPair->Member1
    // psTempPair->Member2
    }

    virtual void BeHappy() = 0;

    R L 2 Replies Last reply
    0
    • R rupeshkp728

      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'

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      The address CSPair[x] is a structure reference not a pointer, hence use the '.' member access operator.

      txtspeak is the realm of 9 year old children, not developers. Christian Graus

      1 Reply Last reply
      0
      • E Eugen Podsypalnikov

        Try this access in your loops :) :

        {
        CheckSumPair* psTempPair = (CheckSumPair*) ((BYTE*)CSPair + sizeof(CheckSumPair) * x);
        // psTempPair->Member1
        // psTempPair->Member2
        }

        virtual void BeHappy() = 0;

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        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

        E 1 Reply Last reply
        0
        • E Eugen Podsypalnikov

          Try this access in your loops :) :

          {
          CheckSumPair* psTempPair = (CheckSumPair*) ((BYTE*)CSPair + sizeof(CheckSumPair) * x);
          // psTempPair->Member1
          // psTempPair->Member2
          }

          virtual void BeHappy() = 0;

          R Offline
          R Offline
          rupeshkp728
          wrote on last edited by
          #5

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

          1 Reply Last reply
          0
          • R rupeshkp728

            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'

            K Offline
            K Offline
            KingsGambit
            wrote on last edited by
            #6

            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++;
            }

            1 Reply Last reply
            0
            • R rupeshkp728

              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'

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #7

              Your code is really ugly.

              rupeshkp728 wrote:

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

              Why do you use #define instead of typedef?

              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 no StrongCSString 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]

              1 Reply Last reply
              0
              • L Lost User

                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

                E Offline
                E Offline
                Eugen Podsypalnikov
                wrote on last edited by
                #8

                In this case - it is necessary, since sizeof(CheckSumPair*) != sizeof(CheckSumPair) ! :)

                virtual void BeHappy() = 0;

                L 1 Reply Last reply
                0
                • R rupeshkp728

                  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'

                  U Offline
                  U Offline
                  User 3919723
                  wrote on last edited by
                  #9

                  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 char

                  typedef 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;
                  

                  }

                  1 Reply Last reply
                  0
                  • E Eugen Podsypalnikov

                    In this case - it is necessary, since sizeof(CheckSumPair*) != sizeof(CheckSumPair) ! :)

                    virtual void BeHappy() = 0;

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    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

                    E 1 Reply Last reply
                    0
                    • L Lost User

                      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

                      E Offline
                      E Offline
                      Eugen Podsypalnikov
                      wrote on last edited by
                      #11

                      Yes, they can !!! :-D You are right, Richard ! (probably I was intricated by malloc... :-D ) Thank you very much ! :thumbsup:

                      virtual void BeHappy() = 0;

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups