Thread: counting rows in a 2D array

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    counting rows in a 2D array

    Okay im a complete newbie when it comes to C. I need to write a function that takes a 2D array as an input and then counts the number of rows in the 2D array. I have played around a bit and did some random testing but i keep hitting a wall(C not keeping track of array boundaries!). The general idea behind the code is simple and i just find it hard to believe something so simple is impossible to implement. HELP!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The arrays size, including it's number of rows, are fixed, when you create the array.

    You need to post some code, because what you're saying, doesn't agree with da C.

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    size_t element_count = sizeof(array) / sizeof(*array);

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please show us what you've tried.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by msh View Post
    Code:
    size_t element_count = sizeof(array) / sizeof(*array);
    But he's asking for the number of rows, instead of elements.

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Adak View Post
    But he's asking for the number of rows, instead of elements.
    Ambiguous variable naming on my part. In an array of arrays (rows), rows are the elements. At least that's how I understand it.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    True ^^^, but the OP specified a 2D array.

    Wriggle room is very limited here!

  8. #8
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Adak View Post
    True ^^^, but the OP specified a 2D array.

    Wriggle room is very limited here!
    Fair enough. But now I can't edit because you already quoted it.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Angry

    alright thanks guys for the quick response. Below is some code iv'e tried.

    char str[3][3] ={"1","2","3"};
    int i = 0;
    while(str[i] != NULL || *str[i] = '\0')
    {
    i++
    }

    this however doesn't work at all. As apparently the out of boundary areas posses values.
    Help?

  10. #10
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code for what you want was already posted; read the bloody thread.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Oh!.... Sorry there!
    I couldn't tell if what you posted, was exactly what you wanted( you stated that you couldn't edit it?).
    either way thanks a lot!
    Testing it out right now.
    Last edited by Kyoukoku; 09-22-2010 at 08:10 AM.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Kyoukoku View Post
    alright thanks guys for the quick response. Below is some code iv'e tried.

    char str[3][3] ={"1","2","3"};
    int i = 0;
    while(str[i] != NULL || *str[i] = '\0')
    {
    i++
    }

    this however doesn't work at all. As apparently the out of boundary areas posses values.
    Help?
    Use code tags [code][/code] when posting code samples.

    str[i] will never be null since (in this context) it is an address that always exists regardless of how many actual elements exist. If str[0] exists at address 1000 and str[1] exists at 1003 and str[2] exists at 1006, then str[3] exists at 1009 regardless of whether there is a str[3] at all. This is because of simple pointer arithmetic. An example (granted this one is C++) would easily show this:
    Code:
        char str[3][3] = {"1","2","3"};
    
        for( int i = 0; i < 5; ++i )
        {
            std::cout << "str[" << i << "] is : " << str[i];
            std::cout << " the address is: " << reinterpret_cast<void*>(str[i]) << std::endl;
        }
    Output is below, of note just look at the address listed. It simply increases by 3 even though the elements beyond str[2] do not exist.
    Last edited by hk_mp5kpdw; 09-22-2010 at 08:32 AM. Reason: Can't count 1,2,4?
    "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

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    char str[3][3] ={"1","2","3"};
    int i = 0;
    while(str[i] != NULL || *str[i] = '\0')
    {
    i++
    }
    I wonder
    char str[3][3]


    if you
    char str[3][3]


    could tell me
    char str[3][3]


    how many
    char str[3][3]


    rows are in
    char str[3][3]


    my char array?

    char str[3][3]


    Help?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need help with this question:
    By marcuspax in forum C Programming
    Replies: 16
    Last Post: 08-16-2010, 08:12 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 02:01 AM
  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