# Thread: counting rows in a 2D array

1. ## 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. 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. Code:
`size_t element_count = sizeof(array) / sizeof(*array);`

4. Please show us what you've tried.

5. Originally Posted by msh
Code:
`size_t element_count = sizeof(array) / sizeof(*array);`
But he's asking for the number of rows, instead of elements.

6. Originally Posted by Adak
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. True ^^^, but the OP specified a 2D array.

Wriggle room is very limited here!

8. Originally Posted by Adak
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. 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. Code for what you want was already posted; read the bloody thread.

11. 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.

12. Originally Posted by Kyoukoku
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.

13. 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