Copying a row of a two dimensional array into a one dimensional array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • v859
    New Member
    • May 2010
    • 9

    #1

    Copying a row of a two dimensional array into a one dimensional array

    I am trying to string copy using strcpy, a row of a two dimensional array into a one dimensional array.

    Can I do this?

    Code:
        void function(char tempArray[][10])
    {
        int i;
         int j;
         string minimum;
       
    
         for(i=0; i< MAX_SIZE; i++)
        {
             location = i;
             strcpy(minimum,tempArray[i]);
         }
    
    }
    How about this?

    Code:
        void function(char tempArray[][10])
    {
        int i;
         int j;
         char minimum[];
       
    
         for(i=0; i< MAX_SIZE; i++)
        {
             location = i;
             strcpy(minimum[],tempArray[i]);
         }
    
    }
  • v859
    New Member
    • May 2010
    • 9

    #2
    I didn't mean for that to be in a for loop.

    I meant to say

    Code:
    char tempArray[10][10];
    string minimum;
    
    int i = 4;
    int location = i;
    strcpy(minimum, tempArray[i]);
    or

    Code:
    char tempArray[10][10];
    char minimum[]
    
    int i=4;
    strcpy(minimum,tempArray[i]);
    Last edited by v859; May 12 '10, 03:21 AM. Reason: Missing ")" character

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      What happens when you compile and run the code?

      Comment

      • v859
        New Member
        • May 2010
        • 9

        #4
        I did get a can not convert char to char error message, but that is when I had

        Code:
        char temporary;
        char array[10][10];
        
        strcpy(temporary, array[10]);
        Which is now obvious why I got that error, trying to put a string into a single char variable.

        I don't know what happens when I try it the ways I listed above, since I do not have a C++ compiler on this computer.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          strcpy() starts copying from the address of the second argument and doesnt't stop until it hits a binary zero.

          Therefore, using strcpy() in this fashion will never guarantee that 10 bytre are copied.

          Is using strcpy() a homework requirement? If not, I sugget using strncpy() and tell it to copy only 10 bytes.

          Since all arrays are one dimensional, you need only locate the starting address and copy 10 elements from there.

          Comment

          • v859
            New Member
            • May 2010
            • 9

            #6
            I was able to solve those problems. Thanks for the help. I have a new problem though that I will post about in a new thread.

            Comment

            Working...