Assigning the value of a const array to a normal array

This is a discussion on Assigning the value of a const array to a normal array within the C++ Programming forums, part of the General Programming Boards category; I'm wondering if there is a way to do something like this: Code: const int constArray[3][3] = {0, 1, 0, ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    5

    Assigning the value of a const array to a normal array

    I'm wondering if there is a way to do something like this:

    Code:
    const int constArray[3][3] = {0, 1, 0, 1, 1, 1, 0, 1, 0};
    int myArray[3][3];
    
    ...
    myArray = constArray;
    ...
    Obviously I could just loop over the arrays and set each element, but that seems inefficient. I'm not suprised that the above doesn't work. I half-heartedly tried assigning the address of myArray to the address of constArray but that didn't work either. I've got a few other ideas bouncing around but I thought I'd ask and see if anyone has a particularly good way of doing this before I do something strange (as I'd done a fair bit of searching and didn't turn up anything).

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    I think i know what you're looking for, but unfortunately don't know how to do it yet with arrays. It's called operator overloading (you'll want to overload the '=' operator). Sorry I can't help you much.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "you'll want to overload the '=' operator"

    Hmmm..where are classes involved?

    Array names are constant pointers, i.e you cannot change their addresses, which is what this line tries to do:

    myArray = constArray;
    Last edited by 7stud; 08-11-2003 at 02:04 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Hmmm..where are classes involved?
    Thought you could do it w/o classes?
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    Thanks for the help guys. Both of Salem's struct and memcpy methods worked (memcpy was one I had in mind to try...the struct one was new to me), and I think I'll end up using memcpy because it seems redudant to keep a class around that contains nothing more than one member (thus forcing me to say foo.array[][] everytime I want something out of it).

    I'm using this approach because each instance of the class that owns this array will start with the contents being something, but the contents will likely change based on each instance. If they were not going to change once initialized, but had dozens of possible initial configurations, I'd think keeping a pointer around to point to the appropriate const array initial set up would be the way to go. Is there a way to set a pointer to a const array? My quick experiment failed, something like this:

    Code:
    int *ptr = 0;
    const int array[2][2] = {0, 1, 1, 0};
    
    ...
    ptr = array;
    ...

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ...maybe this would be a more illustrative example:
    Code:
    const int (*ptr)[2] = 0;
    const int array[6][2] = { { 0, 1 }, { 1, 0 } };

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    Thanks for the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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