Thread: Initializing a 2D Array in C

  1. #16
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Point. And yes, the first example, the nested for loops are the most readable, IMO. Besides a call to memset(), as you point out so nicely.

    Note that optimizations really mess up GDB, however. If you're working on a project that has lots of calculations to go through before it reaches a breakpoint, speed is always nice to have.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  2. #17
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by Cell View Post
    Hey guys,

    I am trying to initialize a 2D array in C. For example, I have the matrix:

    0 1 0 0
    0 1 1 0
    1 0 1 1
    0 1 1 1

    Now I think this would work:

    Code:
    int A[4][4];
    
    A[0][0] = 0;
    A[0][1] = 1;
    A[0][2] = 0;
    A[0][3] = 0;
    A[0][4] = 0;
    A[1][1] = 0;
    A[1][2] = 1;
    ... ETC....
    A[4][4] = 1;
    But this is a real pain. I will have larger matrices like 17x17 and doing this would be a real annoyance.

    Is there a way to do something like:
    Code:
    int  A[4][4] = {0 1 0 0; 0 1 1 0, ... etc}
    ?

    Thanks!
    for decently small arrays(4x4 or 5x5) like the ones you'hv shown
    a[4][4]={{1,2,3,4},{3,4,5,6}....} is appropriate enough...

    however when you go for huge arrays like the SBOX of Triple-DES or AES algorithm then you'd find it painful to show SBOXes in the same fashion as shown above...

    You'd be much better off passing all of them as single dimentional array...
    such as,
    sbox_des[8][4][16]={14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,0,15...... ...}. the compiler would automatically parse the array grouping first 64 elements as sbox[0] and then 4 sets of 16 elements...
    just make sure that you enter the values row-wise , one row at a time, before going to the next row...and you'll be fine...

    this approch is not recommended if someone else would want to examine the array, however it is acceptable if the array is of know constants, whose values are never to be manipulated...such as the SBOXes...

  3. #18
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by vart View Post
    more effitient in what way?
    It's easier on the compiler to translate a pointer and offset compared to array indexing.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by itCbitC View Post
    It's easier on the compiler to translate a pointer and offset compared to array indexing.
    I do not think it necessary to facilitate the work of the compiler, if it makes the code less readable and gives no other benefits, such as performance
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by vart View Post
    I do not think it necessary to facilitate the work of the compiler, if it makes the code less readable and gives no other benefits, such as performance
    That would be true only for the uninitiated, besides it's not the work of the compiler but the choice of the programmer.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by itCbitC
    That would be true only for the uninitiated, besides it's not the work of the compiler but the choice of the programmer.
    In my opinion, a programmer who chooses to use pointer notation with an offset over the equivalent array notation to iterate over an array is uninitiated. It is a different story if a programmer chooses to use pointers over array indices to iterate over an array. (It is still true that a modern optimising compiler would optimise away the difference, but for the latter case it is the choice between an iterator and an index, not merely a choice between pointer notation and array index notation.)
    Last edited by laserlight; 03-21-2009 at 12:35 PM.
    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. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. Pls help:: function initializing for 2d array
    By ypramesh in forum C Programming
    Replies: 3
    Last Post: 03-29-2006, 03:35 PM
  5. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM