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. copy a 2 dimensional array

copy a 2 dimensional array

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
13 Posts 5 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.
  • J John M Drescher

    Dynamic or Static?? John

    J Offline
    J Offline
    johnstonsk
    wrote on last edited by
    #3

    Static. sj

    J 1 Reply Last reply
    0
    • J johnstonsk

      Static. sj

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

      I believe you can use memcpy in the same way you did as a 1 dimensional array. John

      1 Reply Last reply
      0
      • J johnstonsk

        How can I copy a 2dim array? thanks, Steven

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #5

        If it's the same as in your previous question, but in two dimensions, then you can do the same thing

        char a1[10][10];
        char a2[10][10];
        memcpy(a1, a2, sizeof(a2));

        Hope this helps,

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        J 1 Reply Last reply
        0
        • R Ryan Binns

          If it's the same as in your previous question, but in two dimensions, then you can do the same thing

          char a1[10][10];
          char a2[10][10];
          memcpy(a1, a2, sizeof(a2));

          Hope this helps,

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

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

          Thanks, I must be doing something ese wrong, because it keeps crashing. I'll keep pecking :) sj

          R 1 Reply Last reply
          0
          • J johnstonsk

            Thanks, I must be doing something ese wrong, because it keeps crashing. I'll keep pecking :) sj

            R Offline
            R Offline
            Ryan Binns
            wrote on last edited by
            #7

            johnstonsk wrote: must be doing something ese wrong, because it keeps crashing. Post the array declarations, and the code that causes the crash, and we'll see what we can do for you :)

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            J 1 Reply Last reply
            0
            • R Ryan Binns

              johnstonsk wrote: must be doing something ese wrong, because it keeps crashing. Post the array declarations, and the code that causes the crash, and we'll see what we can do for you :)

              Ryan

              "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

              J Offline
              J Offline
              johnstonsk
              wrote on last edited by
              #8

              The error I get is there is a call to undefined function memcpy() I included the string.h Can this function only copy chars or strings? The program that I have reads data from a pci card and puts it in the structures below: This data gets updated 2 times a second.

              struct TSimHeader
              {
              char Name[47][20];
              char Unit[47][20];
              double Min[47];
              double Max[47];
              int SignalCount;
              int SimStatus;

              }static TSimHeader\_arr\[10\];
              
              
              struct TSimSignal
              {
                 	double Value\[47\];
              double TimeStamp;
              
              }static TSimSignal\_arr\[10\];
              

              I have added a new feature to the program that will allow the recording of data. This will be done by writiing the data to a *csv file so it can be read in excel. Since the data in the structures is getting updated 2 times a second I thought that it would be good to just make a new array and copy the data to the array so, if the user wanted to start recording the data it would be there ready to start recording. That is why I call the copyData() function everytime the data is updated. I also created arrays that are of the same type and size of the ones in the struct

              char name[47][20];
              char unit[47][20];
              double min[47];
              double max[47];
              double value[47];

              Below is the copyData() function:

              void RFMAccess::copyData(){

              if(firstTime1){
                  memcpy(pflight\_data->name, TSimHeader\_arr\[0\].Name,
                              sizeof(TSimHeader\_arr\[0\].Name));
                  memcpy(pflight\_data->unit, TSimHeader\_arr\[0\].Unit,
                              sizeof(TSimHeader\_arr\[0\].Unit));
                  memcpy(pflight\_data->min, TSimHeader\_arr\[0\].Min,
                              sizeof(TSimHeader\_arr\[0\].Min));
                  memcpy(pflight\_data->max, TSimHeader\_arr\[0\].Max,
                              sizeof(TSimHeader\_arr\[0\].Max));
              }
              if(!firstTime1){
                   memcpy(pflight\_data->value, TSimSignal\_arr\[0\].Value,
                              sizeof(TSimSignal\_arr\[0\].Value));
              }
              

              }

              Then I call the writeData() function from another class that writes teh data in the copied arrays to a file. Here is the writeData()

              void LogData::writeData(){
              Sleep(350);
              int count;
              if(firstTime){
              fout<<"flight_data,";
              for(int i=0; iname[i]<

              K 1 Reply Last reply
              0
              • J johnstonsk

                The error I get is there is a call to undefined function memcpy() I included the string.h Can this function only copy chars or strings? The program that I have reads data from a pci card and puts it in the structures below: This data gets updated 2 times a second.

                struct TSimHeader
                {
                char Name[47][20];
                char Unit[47][20];
                double Min[47];
                double Max[47];
                int SignalCount;
                int SimStatus;

                }static TSimHeader\_arr\[10\];
                
                
                struct TSimSignal
                {
                   	double Value\[47\];
                double TimeStamp;
                
                }static TSimSignal\_arr\[10\];
                

                I have added a new feature to the program that will allow the recording of data. This will be done by writiing the data to a *csv file so it can be read in excel. Since the data in the structures is getting updated 2 times a second I thought that it would be good to just make a new array and copy the data to the array so, if the user wanted to start recording the data it would be there ready to start recording. That is why I call the copyData() function everytime the data is updated. I also created arrays that are of the same type and size of the ones in the struct

                char name[47][20];
                char unit[47][20];
                double min[47];
                double max[47];
                double value[47];

                Below is the copyData() function:

                void RFMAccess::copyData(){

                if(firstTime1){
                    memcpy(pflight\_data->name, TSimHeader\_arr\[0\].Name,
                                sizeof(TSimHeader\_arr\[0\].Name));
                    memcpy(pflight\_data->unit, TSimHeader\_arr\[0\].Unit,
                                sizeof(TSimHeader\_arr\[0\].Unit));
                    memcpy(pflight\_data->min, TSimHeader\_arr\[0\].Min,
                                sizeof(TSimHeader\_arr\[0\].Min));
                    memcpy(pflight\_data->max, TSimHeader\_arr\[0\].Max,
                                sizeof(TSimHeader\_arr\[0\].Max));
                }
                if(!firstTime1){
                     memcpy(pflight\_data->value, TSimSignal\_arr\[0\].Value,
                                sizeof(TSimSignal\_arr\[0\].Value));
                }
                

                }

                Then I call the writeData() function from another class that writes teh data in the copied arrays to a file. Here is the writeData()

                void LogData::writeData(){
                Sleep(350);
                int count;
                if(firstTime){
                fout<<"flight_data,";
                for(int i=0; iname[i]<

                K Offline
                K Offline
                KeithD
                wrote on last edited by
                #9

                are you sure you're including string.h in the file that uses memcpy? that error you're getting means it can't find the memcpy function at all.

                J 1 Reply Last reply
                0
                • K KeithD

                  are you sure you're including string.h in the file that uses memcpy? that error you're getting means it can't find the memcpy function at all.

                  J Offline
                  J Offline
                  johnstonsk
                  wrote on last edited by
                  #10

                  Yes it is definetly in there. The program will compile but, if I run the debugger on the memcpy() function it reads that the function is undefined. Strange. Steven

                  K J 2 Replies Last reply
                  0
                  • J johnstonsk

                    Yes it is definetly in there. The program will compile but, if I run the debugger on the memcpy() function it reads that the function is undefined. Strange. Steven

                    K Offline
                    K Offline
                    KeithD
                    wrote on last edited by
                    #11

                    well you can try a double for loop to copy the data. for(int i=0; i<47; i++) for(int j=0; j<20; j++) old[i][j] = new[i][j]; i wouldn't recomend that (memcpy should work) but at least you can get your debugger going again to examine the memory. if it still crashes then you have some other problem =/

                    1 Reply Last reply
                    0
                    • J johnstonsk

                      Yes it is definetly in there. The program will compile but, if I run the debugger on the memcpy() function it reads that the function is undefined. Strange. Steven

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

                      johnstonsk wrote: I run the debugger on the memcpy() function it reads that the function is undefined. :confused::confused::confused: What do you mean it is undefined?? If it compiled then it is defined... You mean that the debugger will not step into the memcpy() function? If so you do not have the correct symbols installed.. John

                      1 Reply Last reply
                      0
                      • J johnstonsk

                        How can I copy a 2dim array? thanks, Steven

                        A Offline
                        A Offline
                        armentage
                        wrote on last edited by
                        #13

                        johnstonsk wrote: How can I copy a 2dim array? CIS101 [Introduction to Computer Science]

                        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