2d array definitions

This is a discussion on 2d array definitions within the C++ Programming forums, part of the General Programming Boards category; This works: Code: int vector[2][3] = {{5,5,5},{5,5,5}}; This does not work: Code: int vector[2][3]; vector = {{5,5,5},{5,5,5}}; I've toyed around ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    2d array definitions

    This works:

    Code:
    int vector[2][3] = {{5,5,5},{5,5,5}};
    This does not work:

    Code:
    int vector[2][3];
    vector = {{5,5,5},{5,5,5}};
    I've toyed around with it some, but I cannot figure out what I'm doing wrong.

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Quote Originally Posted by jsrig88 View Post
    This works:

    Code:
    int vector[2][3] = {{5,5,5},{5,5,5}};
    This does not work:

    Code:
    int vector[2][3];
    vector = {{5,5,5},{5,5,5}};
    I've toyed around with it some, but I cannot figure out what I'm doing wrong.
    The way you are trying to initialize in the first one can only be done when you are declaring an array, which is why the first one works. In order to initialize after you declare it, you must do it by iterating through the array one by one.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  3. #3
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    When looking deeper, here:
    Code:
    vector = {{5,5,5},{5,5,5}};
    vector is just an address of first element in an array of arrays. It makes no sense to try to change the poiner to {{5,5,5},{5,5,5}}.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    100
    You have got to be kidding me. I know that you can use an array's name as a pointer to that array, so I thought that I could use the pointer and define it thusly. It surprises me that it doesn't work that way.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by jsrig88 View Post
    You have got to be kidding me. I know that you can use an array's name as a pointer to that array, so I thought that I could use the pointer and define it thusly. It surprises me that it doesn't work that way.
    array is not a pointer, there are situations when array name is automatically casted to the pointer to the first element of the array...

    but you cannot modify it

    it just like saying that because 2+3 is int you think it is logical to assign to it some other value like
    2+3 = 6
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

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