Length of 'char* numberlist[]' in C and C++

Go To StackoverFlow.com

0

Try to find out how to get the total number of items in char* numberlist[] in C and C++, the following is the code:

const char* list_of_filename[] = {"One","Two","Three","Four","Five"};
2012-04-05 01:51
by Fusionmate


7

The standard idiom for finding the number of elements in an array is

int num_items = sizeof(list_of_filename) / sizeof(list_of_filename[0]);
2012-04-05 01:53
by Adam Liss


2

How about:

sizeof(list_of_filename) / sizeof(char*)

Or more generally:

sizeof(list_of_filename) / sizeof(list_of_filename[0])
2012-04-05 01:54
by Timothy Jones
Rather than duplicating the previous answer, you might suggest sizeof(list_of_filename) / sizeof(*list_of_filename)Adam Liss 2012-04-05 01:56
I think we answered at the same time :) I personally prefer the [0] version, so I wouldn't feel right about editing in the other version. Feel free to edit it into your answer though - I gave you an upvote : - Timothy Jones 2012-04-05 02:02
Ads