Thread: Initialising multidimensional arrays

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    14

    Question Initialising multidimensional arrays

    So, I have this :

    Code:
    #include <iostream>
    using namespace std;
    int main(){
    	char chr[1][256];
    	chr[0] = "hello everyone!";
    	chr[1] = "hello everyone!1";
    	cout<< &chr[0] << "\n" << &chr[1];
    	cin.get();
    }
    I am trying to initialise a multi dimensional c-style string, and then output it. But I get "Error E2277 chr.cpp 5:Lvalue required in function main()" when I try to compile it. The same error appears for the line 6, by the way.

    Could anybody tell me what`s wrong about it ? I can`t figure out anything.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. If you declare char chr[1][256], you don't have two rows in your multi-dimensional arrays, just one.
    2. Once you hit the semicolon on the same line, it is then too late to do any initializing. You have to initialize when you declare:
    Code:
    char chr[2][256] = {"hello everyone!", "hello everyone!1"};

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    14

    Smile

    Quote Originally Posted by tabstop View Post
    1. If you declare char chr[1][256], you don't have two rows in your multi-dimensional arrays, just one.
    I thought of that, but I was not sure, though...

    Quote Originally Posted by tabstop View Post
    2. Once you hit the semicolon on the same line, it is then too late to do any initializing. You have to initialize when you declare:
    Code:
    char chr[2][256] = {"hello everyone!", "hello everyone!1"};
    Thanks, it works now.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    When declaring a multi dimensional array keep this formula in mind.
    ARRAY [ROWS] [COLUMNS] , that is A [2] [5] means that array A has 2 rows and 5 columns. By the way I don't recommend you using cin.get(); , it sometimes doesn't work as it is supposed to. Better not use it as intended in here. Try alternate to this,

    system("pause"); defined in cstdlib.
    Last edited by saqib_; 04-07-2010 at 08:40 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by saqib_
    By the way I don't recommend you using cin.get(); , it sometimes doesn't work as it is supposed to.
    It should work as it is supposed to. When it does not work, it is because you failed to use it correctly, e.g., if your intention is to use it to pause execution after reading some input where the newline may be left in the buffer, then cin.ignore() should also be used.

    Quote Originally Posted by saqib_
    Try alternate to this,

    system("pause"); defined in cstdlib.
    The system function is part of the standard library, but whether "pause" does what you expect is another matter: "it sometimes doesn't work as it is supposed to."
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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. Multidimensional Arrays in a function prototype
    By Enanito01478 in forum C Programming
    Replies: 2
    Last Post: 10-11-2009, 01:10 AM
  2. Multidimensional arrays
    By Niels_M in forum C Programming
    Replies: 51
    Last Post: 09-12-2009, 03:16 PM
  3. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  5. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM