Thread: 2-D Array Initialization error (Header File)

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    Visakhapatnam, India
    Posts
    7

    2-D Array Initialization error (Header File)

    Hello,

    I want to use the contents of a 2-D array in different .cpp files

    So, I taught of doing it with a header file.
    Code:
    (In XYZ.h)
    int abc[5][20];
    abc[5][5]=98; // This is not valid
    Direct initialization outside a funtion is Not Allowed and so "abc[5][5]=98;" is giving an error.
    I want to use that array in different .cpp files
    So how should I do this?

    Also i have another question
    If my array is filled in this way
    abc[0][] -- Has 20 elements
    abc[1][] -- actually needs only abc[1][10] (10 elements are enough)
    So is it possible to dynamically do it to save that extra space used.

    Thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can't have code statements such as the assignment you are attempting outside of a function. You can initialize at the point of declaration/instantiation however:
    Code:
    // Good (inside or outside of a function block)
    int ar[2][2] = {{2,4},{8,16}};
    
    
    // Bad (outside of a function block)
    int ar[2][2];
    ar[0][0] = 2;
    ar[0][1] = 4;
    ar[1][0] = 8;
    ar[1][1] = 16;
    If you want the later rather than the former, you'll need to put those assignment statements in a function of some kind that you would use to initialize the array. On a side note to that, its usually a bad idea to declare variables in a header file, especially a header that is going to be included in multiple source files unless said variable is declared extern.

    As for your other array question. Do you really need to save 40 bytes of space? If you don't need to use all the space then simply don't use it. The extra 40 bytes for that array that you think are wasted are probably easier to deal with than the complexities involved in the various solutions to the problem.
    Last edited by hk_mp5kpdw; 01-12-2012 at 08:10 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Visakhapatnam, India
    Posts
    7
    Thanks
    Code:
    int ar[2][2] = {{2,4},{8,16}};
    Ok i will do it this way in the Header File ..

    And coming to the second question
    My array is of size 600*300 and of type Long Long
    so each is of 64bits length and 8bytes size.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by hk_mp5kpdw View Post
    On a side note to that, its usually a bad idea to declare variables in a header file, especially a header that is going to be included in multiple source files unless said variable is declared extern.
    I think you missed reading this.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I want to use the contents of a 2-D array in different .cpp files
    > So, I taught of doing it with a header file.
    Well if you put the array definition in the header file, then EVERY source file is going to get a complete copy. Given the size you propose, this is a big waste of memory.

    Code:
    // in the .h file
    extern long long array[600][300];
    Code:
    // in exactly ONE .cpp file
    long long array[600][300] = {
      { // 300 elements separated by commas },
      { // another 300 },
      { // only 598 more rows to go },
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Salem View Post
    > I want to use the contents of a 2-D array in different .cpp files
    > So, I taught of doing it with a header file.
    Well if you put the array definition in the header file, then EVERY source file is going to get a complete copy. Given the size you propose, this is a big waste of memory.
    And will cause a multiple definitions linker error
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array initialization error
    By FlyingShoes12 in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2009, 11:07 AM
  2. initialization lists in header files?
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 10:20 PM
  3. Replies: 2
    Last Post: 02-28-2008, 11:51 PM
  4. New to C++, Header File Error
    By kspill2 in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2007, 02:34 PM
  5. header file error
    By beon in forum C Programming
    Replies: 7
    Last Post: 11-16-2006, 07:33 AM