C sizeof char* array

Ayman picture Ayman · Oct 13, 2009 · Viewed 99.1k times · Source

I have a char* array as follows:

char *tbl[] = { "1", "2", "3" };

How do I use the sizeof operator to get the number of elements of the array, here 3?

The below did work, but is it correct?

int n = sizeof(tbl) / sizeof(tbl[0]) 

Answer

sharptooth picture sharptooth · Oct 13, 2009

Yes,

size_t n = sizeof(tbl) / sizeof(tbl[0])

is the most typical way to do this.

Please note that using int for array sizes is not the best idea.