2-D Array Initialization error (Header File)

This is a discussion on 2-D Array Initialization error (Header File) within the C++ Programming forums, part of the General Programming Boards category; Hello, I want to use the contents of a 2-D array in different .cpp files So, I taught of doing ...

  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,794
    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 07: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
    2,521
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    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, 10: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, 10: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, 06:33 AM

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