By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,615 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,615 IT Pros & Developers. It's quick & easy.

strlen

P: n/a
Hello All

I have a 2D array,

char arr[4][10] = {"Hello","world","test","hi"};

how can find the strlen of the array

am expecting array length as "4"

Thanks in Adv
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Imran" <im******@in.bosch.com> wrote in message
news:cf**********@ns1.fe.internet.bosch.com...
Hello All

I have a 2D array,

char arr[4][10] = {"Hello","world","test","hi"};

how can find the strlen of the array

am expecting array length as "4"

Thanks in Adv


Your expected answer indicates that you are really trying to determine the
first dimension of a multi-dimensional array. The strlen function does not
do this. In what context are you trying to determine this? If you're
inside a function, then you may have to pass the first dimension to that
function. For example:

void foo(char (*arr)[10], int size) {
for ( int i = 0; i < size; ++i ) {
std::cout << arr[i] << std::endl;
}
}

int main() {
char arr[4][10] = {"Hello","world","test","hi"};
foo(arr,sizeof(arr)/sizeof(arr[0]));
return 0;
}

--
David Hilsee
Jul 22 '05 #2

P: n/a
"Imran" <im******@in.bosch.com> wrote in message news:<cf**********@ns1.fe.internet.bosch.com>...
Hello All

I have a 2D array,

char arr[4][10] = {"Hello","world","test","hi"};

how can find the strlen of the array

am expecting array length as "4"


One possibility when you're truly working with an array (i.e. NOT a
pointer) is something like:

#define elements(x) ((sizeof(x)/sizeof(x[0]))

Keep in mind, however, that an array will decay to a pointer anytime
it is passed as a parameter, so this can really only be applied to
globals, or else inside of the function where the array is defined.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.