Thread: Initializing a 2D Array in C

  1. #1
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72

    Initializing a 2D Array in C

    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!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    A[3][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Or, for larger 2D arrays . . .

    Code:
    int array[100][100];
    int x, y;
    for(x = 0; x < 100; x ++) {
        for(y = 0; y < 100; y ++) array[x][y] = 0;
    }
    Better than a few thousand chars of static initializers
    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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Nightowl View Post
    Or, for larger 2D arrays . . .

    Code:
    int array[100][100];
    int x, y;
    for(x = 0; x < 100; x ++) {
        for(y = 0; y < 100; y ++) array[x][y] = 0;
    }
    Better than a few thousand chars of static initializers
    But if you are setting all elements to zero, just doing
    Code:
    int array[100][100] = { {0 } };
    works just fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Good point. Though the for loop can be used at non-initialization times, at any point in the program. And, to me, the for loop is a bit clearer.
    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.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    You'll learn malloc() next and it'll shine a whole new light on this

  7. #7
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Quote Originally Posted by shiryu3 View Post
    You'll learn malloc() next and it'll shine a whole new light on this
    shiryu, that is overly condescending. Also, you should specify who you are talking to . . .

    And yeah, you are correct. That is the code I use for allocated arrays.
    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.

  8. #8
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Thank you for the answers, guys.

    However, for this code:

    Quote Originally Posted by Nightowl View Post
    Or, for larger 2D arrays . . .

    Code:
    int array[100][100];
    int x, y;
    for(x = 0; x < 100; x ++) {
        for(y = 0; y < 100; y ++) array[x][y] = 0;
    }
    Better than a few thousand chars of static initializers
    This would just fill the matrix with zeroes. How can this be used to fill a matrix with specific values?

    Also, I do know about malloc(). I don't know how I can use it to create a matrix with specified values, though. I also don't see what benefits it has over what brewbuck mentioned in his post.

    I know there are plenty of ways to create a matrix filled with 0s, but I'd like to see how there can be a more efficient way to fill a matrix with specific values than what brewbuck posted.

    Thanks again for the help.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > but I'd like to see how there can be a more efficient way to fill a matrix with specific values than what brewbuck posted.

    Nope. Nothing more efficient than that, as far as initialisation/setting goes.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Cell View Post
    ... but I'd like to see how there can be a more efficient way to fill a matrix with specific values than what brewbuck posted...
    A more efficient way would be to use the pointer and offset version.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by itCbitC View Post
    A more efficient way would be to use the pointer and offset version.
    more effitient in what way?
    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

  12. #12
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Think on it. Having two loops rather than one? . . .
    Code:
    int array[100][100];
    int x;
    for(x = 0; x < sizeof(int)*100*100; x ++) *(array+x) = 0;
    . . . should be faster than what I posted before.

    Just more confusing, that's all . . .
    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.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Nightowl View Post
    Think on it. Having two loops rather than one? . . .
    Code:
    int array[100][100];
    int x;
    for(x = 0; x < sizeof(int)*100*100; x ++) *(array+x) = 0;
    . . . should be faster than what I posted before.

    Just more confusing, that's all . . .
    things like that should not be "thinked" - or exemine asm code or - better - use profiler.

    I suppose both loops will be replaces by memset and will have same time exactly
    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

  14. #14
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    If using a good compiler such as gcc, with all optimizations turned on, certainly. However, I don't think that gcc would do that kind of level of optimization by default . . .

    Besides, the second one is probably almost exactly how memset() is implemented . . .
    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.

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Nightowl View Post
    If using a good compiler such as gcc, with all optimizations turned on, certainly. However, I don't think that gcc would do that kind of level of optimization by default . . .

    Besides, the second one is probably almost exactly how memset() is implemented . . .
    why to bother about performance is you do not turn optimization on?

    if you debug something- you'd like to have most readable code, not the quickets, step-by-step has no gain from speed here.

    and I doubt that memset is written with the loop like this - asm could move chunks of bytes longer than int in one instruction...
    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

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