strlen

I

Imran

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
 
D

David Hilsee

Imran said:
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 << std::endl;
}
}

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

Jerry Coffin

Imran said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top