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. Pointer to structure problem...

Pointer to structure problem...

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
12 Posts 7 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.
  • I IrishSonic

    Hiya I have to structure and wanted to make a pointer to it WITH 10 INSTANCES. So I have: struct s_Test { char name[30]; } struct s_Test* Test[10]; void main() { memcpy( Test[0]->name,"Paul",30 ); // system error here return; } My problem happens when I go to access an element of the structure. I gives me the system error that the app must close. Any ideas how to access them properly?? Thanks;

    D Offline
    D Offline
    Dominik Reichl
    wrote on last edited by
    #3
    1. The source buffer for memcpy is too small. It is just 5 characters ("Paul"+zero), but you specify 30. This will result in a memory access error. A valid value for memcpy source len would be strlen("Paul")+1. 2) Declare the Test array as array and not as pointer array. So instead of struct s_Test* Test[10]; do a struct s_Test Test[10]; 3) If you have done step 2 you don't need a dereferenced access to the name member variable any more. So the memcpy would look like: memcpy(Test[0].name,"Paul",strlen("Paul")+1); :-D -Dominik

    _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;)

    I 1 Reply Last reply
    0
    • I IrishSonic

      Hiya I have to structure and wanted to make a pointer to it WITH 10 INSTANCES. So I have: struct s_Test { char name[30]; } struct s_Test* Test[10]; void main() { memcpy( Test[0]->name,"Paul",30 ); // system error here return; } My problem happens when I go to access an element of the structure. I gives me the system error that the app must close. Any ideas how to access them properly?? Thanks;

      R Offline
      R Offline
      RChin
      wrote on last edited by
      #4

      .. and you rightly deserve an error message! The variable Test has been defined as a pointer to the structure:
      struct s_Test* Test[10] Therefore the memory location for your structure has not been allocated as yet.

      try:
      .. .. struct s_Test Test[10]; void main() { // use the appropriate string Function. strcpy( Test[0].name, "Paul" ); return; }


      "Wise men talk because they have something to say; fools, because they have to say something."
      Plato

      B 1 Reply Last reply
      0
      • J jhwurmbach

        Try to use a std:vector of std:string. That works. If you really cant use C++ and MUST continue to use C, then this one works:

        #include "stdafx.h"
        #include < memory.h >

        struct s_Test
        {
        char name[30];
        };

        int main(int argc, char* argv[])
        {
        struct s_Test Test[10];
        memcpy( Test[0].name,"Paul",30 );
        return 0;
        }


        My opinions may have changed, but not the fact that I am right.

        D Offline
        D Offline
        Dominik Reichl
        wrote on last edited by
        #5

        Meeeep. System error. Copying 30 characters from a buffer which is only 5 characters ("Paul"+zero) long. :-D :beer: -Dominik


        _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;)

        J 1 Reply Last reply
        0
        • D Dominik Reichl

          Meeeep. System error. Copying 30 characters from a buffer which is only 5 characters ("Paul"+zero) long. :-D :beer: -Dominik


          _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;)

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #6

          OOOPS! :omg: But it did compile.....:-O That type of errors is the reason why I prefer std::vector and std::string.


          My opinions may have changed, but not the fact that I am right.

          1 Reply Last reply
          0
          • D Dominik Reichl
            1. The source buffer for memcpy is too small. It is just 5 characters ("Paul"+zero), but you specify 30. This will result in a memory access error. A valid value for memcpy source len would be strlen("Paul")+1. 2) Declare the Test array as array and not as pointer array. So instead of struct s_Test* Test[10]; do a struct s_Test Test[10]; 3) If you have done step 2 you don't need a dereferenced access to the name member variable any more. So the memcpy would look like: memcpy(Test[0].name,"Paul",strlen("Paul")+1); :-D -Dominik

            _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;)

            I Offline
            I Offline
            IrishSonic
            wrote on last edited by
            #7

            Thanks for all of those suggestions and help. I know I am being awkward but I really want to get it working using a pointer to the structure. I could do it the normal way no problem but I only want to allocate the memory as I need it. I don't want it to be a static array. Is it not possible to have a pointer to a structure with instances aswell??

            D R J 3 Replies Last reply
            0
            • I IrishSonic

              Thanks for all of those suggestions and help. I know I am being awkward but I really want to get it working using a pointer to the structure. I could do it the normal way no problem but I only want to allocate the memory as I need it. I don't want it to be a static array. Is it not possible to have a pointer to a structure with instances aswell??

              D Offline
              D Offline
              Dominik Reichl
              wrote on last edited by
              #8

              Sure, this is possible too:

              struct s_Test
              {
              char name[30];
              };

              s_Test *Test;

              void main()
              {
              Test = new s_Test[10];

              memcpy(Test\[0\].name,"Paul",strlen("Paul")+1);
              
              // Do something more
              
              delete \[\]Test;
              Test = NULL;
              return;
              

              }

              :-D -Dominik


              _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;)

              1 Reply Last reply
              0
              • I IrishSonic

                Thanks for all of those suggestions and help. I know I am being awkward but I really want to get it working using a pointer to the structure. I could do it the normal way no problem but I only want to allocate the memory as I need it. I don't want it to be a static array. Is it not possible to have a pointer to a structure with instances aswell??

                R Offline
                R Offline
                Rage
                wrote on last edited by
                #9

                If you need a table, you need a table. How do you understand a pointer to a structure with instances as well ? To be able to point at something, you need that the something exist in the memory. It is therefor impossible to have a table without allocating some memory for it. ~RaGE();

                I 1 Reply Last reply
                0
                • R RChin

                  .. and you rightly deserve an error message! The variable Test has been defined as a pointer to the structure:
                  struct s_Test* Test[10] Therefore the memory location for your structure has not been allocated as yet.

                  try:
                  .. .. struct s_Test Test[10]; void main() { // use the appropriate string Function. strcpy( Test[0].name, "Paul" ); return; }


                  "Wise men talk because they have something to say; fools, because they have to say something."
                  Plato

                  B Offline
                  B Offline
                  BhaskarBora
                  wrote on last edited by
                  #10

                  yup Plato.. what you said is correct ! there the array is infact a array of pointers to "struct s_Test " rather then a array of "struct s_Test ". "Think big, think fast, think ahead. Ideas are no one's monopoly"

                  1 Reply Last reply
                  0
                  • R Rage

                    If you need a table, you need a table. How do you understand a pointer to a structure with instances as well ? To be able to point at something, you need that the something exist in the memory. It is therefor impossible to have a table without allocating some memory for it. ~RaGE();

                    I Offline
                    I Offline
                    IrishSonic
                    wrote on last edited by
                    #11

                    Thanks, have it sorted and know the reason y it can't just do it like that. thanks for all ur help. grahamoj

                    1 Reply Last reply
                    0
                    • I IrishSonic

                      Thanks for all of those suggestions and help. I know I am being awkward but I really want to get it working using a pointer to the structure. I could do it the normal way no problem but I only want to allocate the memory as I need it. I don't want it to be a static array. Is it not possible to have a pointer to a structure with instances aswell??

                      J Offline
                      J Offline
                      John M Drescher
                      wrote on last edited by
                      #12

                      What you really need is a vector or list of structures but this is a little too complicated to start off with. You can look up vector or list in the help for examples if you want. I assume that you will not know how many structures you will have at compile time and that is why you want a dynamic array. You can create a large array of pointers to your structure. This will take 4 bytes per element. Then allocate your structures when you need them. Have an integer keep track of the current number of structures. Try this: struct s_Test { char name[30]; } struct s_Test* Test[10]; int nTestCount = 0; void main() { // Add a struct and initialize Test[nTestCount] = new Test; strcpy( Test[nTestCount]->name,"Paul"); nTestCount++; // Add a struct and initialize Test[nTestCount] = new Test; strcpy( Test[nTestCount]->name,"John"); nTestCount++; for(int i=0; i < nTestCount;i++) { printf("%s\n",Test[i]->name); } return; }

                      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