Trying to assign array values to another array

This is a discussion on Trying to assign array values to another array within the C++ Programming forums, part of the General Programming Boards category; Hey all. I'm new to C++ programing, although not stranger to programing at all (I have a lot of experience ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    Trying to assign array values to another array

    Hey all. I'm new to C++ programing, although not stranger to programing at all (I have a lot of experience in Java and PHP). Anyway, I'm trying to assing values of two-dimensional array to a previously declared array. I'm using the basic formula:

    Let's suppose the array with the values I wanna copy are:
    Code:
    int array1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    and then I have the other array:
    Code:
    int array2[3][3];
    The obvious way of doing it would be:
    Code:
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            array2[i][j] = array1[i][j];
        }
    }
    Yeah, but it ain't working. I'll try to explain my best the results:
    When i = 0 and j = 0, array2 value becomes {{1,0,0},{1,0,0},{1,0,0}}. See the problem already? It assings array1[0][0] to array2[0][0], array2[1][0] and array2[2][0], whereas it should assing it to array2[0][0] only. By the time it reaches i = 0 and j = 2, array2 will be {{1,2,3},{1,2,3},{1,2,3}}.
    When the program has i = 1 and j = 0, it array2 will be {{4,2,3},{4,2,3},{4,2,3}}. You see? It assigned array1[1][0] to array2[0][0], array2[1][0] and array[2][0] again! By the time i = 2 and j = 2, array2 = {{7,8,9},{7,8,9},{7,8,9}}.

    If you think of both arrays as matrices, when an element is assinged from the first matrix to the second, it is assigned to all lines in that collumn instead of the desired line and collumn.

    So, I kindly ask you guys: WHAT THE HELL IS GOING ON?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    It looked correct to me so I tested with:
    Code:
    #include <iostream>
    
    int main()
    {
        int array1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
        int array2[3][3];
    
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                array2[i][j] = array1[i][j];
            }
        }
    
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                std::cout << array2[i][j] << ' ';
            }
        }
        std::cout << std::endl;
    }
    The output was as expected:
    1 2 3 4 5 6 7 8 9

    I suggest you post the smallest and simplest compilable program that demonstrates the error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Alright! Tried to mimick my code with the least amount of complexity and found out what the problem was. It is related to inheritance.

    Anyway, here's the deal:

    array2 is declared but not initialized in a base class. Several derived classes will carry what the array dimensions will be and since I can't declare the an array without dimension, I'm declaring it as
    Code:
    //base.h
    int array2[0][0];
    I wanna assign those values (dimensions) to it in the derived class, so what I was trying to do was
    Code:
    //derived.h
    int array2[3][3];
    And that caused the problem, when I changed the dimensions of the array in the base class to match those in the derived class, it worked.

    So my actual problem is: how can I overwrite this array in the derived class with new dimensions? I need it declared in the base class since I don't wanna redefine or move some functions in the base class to the derived class that use this array (although I could do that and it would probably work), but the only way to assign its dimension and values is in the derived class.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    So my actual problem is: how can I overwrite this array in the derived class with new dimensions? I need it declared in the base class since I don't wanna redefine or move some functions in the base class to the derived class that use this array (although I could do that and it would probably work), but the only way to assign its dimension and values is in the derived class.
    You probably should use dynamically allocated arrays here, or better yet, a vector of vectors.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Quote Originally Posted by edhc44 View Post
    So my actual problem is: how can I overwrite this array in the derived class with new dimensions?
    You can't do that. If you define a variable with the same name in a derived class, it will only hide the one in the base class (read: there will be two variables named the same name, but the derived can only access its own variable and the base can still access its own). So there will be two variables with different dimensions but same name. So what you need to do is, like laserlight points out, to use dynamic memory, new and delete. And preferably also a smart pointer such as boost's shared_ptr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Quote Originally Posted by Elysia View Post
    You can't do that. If you define a variable with the same name in a derived class, it will only hide the one in the base class (read: there will be two variables named the same name, but the derived can only access its own variable and the base can still access its own). So there will be two variables with different dimensions but same name.
    So, there's one thing I'm already doing wrong...

    I will post the compilable code I made to help others understand my problem:

    Code:
    //base.h
    class base{
    
        public:
            virtual void createArray();
    
        protected:
            int array[3][3];
    };
    Code:
    //derived.h
    #include "base.h"
    class derived:public base{
    
        private:
            int array[3][3];
    
    };
    Code:
    //derived.cpp
    #include "derived.h"
    #include <iostream>
    
    void base::createArray(){
        int temp[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                this->array[i][j] = temp[i][j];
            }
        }
    
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                std::cout << this->array[i][j] << ' ';
            }
        }
        std::cout << std::endl;
    }
    Code:
    //main.cpp
    #include <iostream>
    #include "derived.h"
    
    derived *test = new derived();
    
    int main()
    {
        test->createArray();
    }
    This code runs just fine. Although I think I made myself clear in my last post, I'd like to point some things out:
    1) The dimension "3" in the derived class is just an example. Every derived class can have this dimension different from one another.
    2) The dimension "3" in the base class is only there in order to match the dimension in the derived class and make this example code run.

    So there you go, I don't know for sure what the dimension will be when I declare private: int array[x][y] in the base class, how can I use dynamically allocated arrays in this situation? Considering what Elysia said, it's worthless declare array AGAIN in the derived class, but as I said before, it is the derived class that actually have those dimensions...

    Maybe this has an easy, simple solution, but as I said, I'm new to C++...

    PS: I actually have a good reason not to use vectors and that's why I'm insisting on arrays.
    Last edited by edhc44; 02-01-2008 at 02:26 PM. Reason: I had post what the reason for not using vectors was, but it wasn't nearly as interesting to read as I thought

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Simply add a pointer to the base class and let all the derived classes inherit it and allocate the memory necessary for your arrays.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would go with the suggestion of using a vector of vectors, that way, you don't have to worry about how they are allocated and such. But a 2D array allocation function looks something like this:

    Code:
    int **Alloc2D(int rows, int cols) 
    {
         int **ptr2D;
         ptr2D = new int* [rows];
         for(int i = 0; i < cols; i++) 
            ptr2D[i] = new int [cols];
         return ptr2D;
    }
    
    int main()
    {
        int **array2d;
        int temp[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    
        array2d = Alloc2D(3, 3);
    
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                array2d[i][j] = temp[i][j];
            }
        }
    
        return 0;
    }
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Quote Originally Posted by matsp View Post
    I would go with the suggestion of using a vector of vectors, that way, you don't have to worry about how they are allocated and such. But a 2D array allocation function looks something like this:

    Code:
    int **Alloc2D(int rows, int cols) 
    {
         int **ptr2D;
         ptr2D = new int* [rows];
         for(int i = 0; i < cols; i++) 
            ptr2D[i] = new int [cols];
         return ptr2D;
    }
    
    int main()
    {
        int **array2d;
        int temp[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    
        array2d = Alloc2D(3, 3);
    
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                array2d[i][j] = temp[i][j];
            }
        }
    
        return 0;
    }
    Thanks dude, problem solved! ^^

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by edhc44 View Post
    Code:
    //base.h
    int array2[0][0];
    You can't have an array with 0 dimension. Turn up your compiler warnings.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    PS: I actually have a good reason not to use vectors and that's why I'm insisting on arrays.
    What was the reason anyway?

    You might want to read Stroustrup's answers to:
    Why are the standard containers so slow?
    How do I deal with memory leaks?
    What's wrong with arrays?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  3. assigning values in file to an array
    By divinyl in forum C++ Programming
    Replies: 9
    Last Post: 07-29-2003, 09:33 AM
  4. getline to store values in an array
    By berenice in forum C++ Programming
    Replies: 6
    Last Post: 07-10-2002, 11:17 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21