31

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]) 
6
  • This was actually answered here And that was the correct way of doing it. Commented Oct 13, 2009 at 12:34
  • 3
    Not if the array was received as a parameter. Check my answer to a similar question. Commented Apr 27, 2012 at 11:16
  • Possible duplicate of How do I determine the size of my array in C? Commented Feb 28, 2013 at 16:14
  • @Elideb: Strictly speaking, an array can't be a parameter. A parameter defined as char *tbl[] is really of type char**; the type is adjusted at compile time. Commented Aug 7, 2013 at 14:57
  • @KeithThompson Yeah, I know, but I still find code around with arrays as parameters, and people expect them to behave as such. I'll try to be more correct when talking about them. Commented Aug 28, 2013 at 17:37

3 Answers 3

40

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.

Sign up to request clarification or add additional context in comments.

19 Comments

Anything wrong with using a size_t for sizes? Or at least something unsigned?
sizeof(char) doesn't save you a dereferencing operation. The compiler calculates size of tbl[0] by understanding its type, and doesn't actually generate code for it. The only thing sizeof(char) instead of sizeof(tbl[0]) would achieve for you is problem in the future if the array changes its type from char to something else.
@Shahbaz I believe once compiler sees sizeof being called for a variable size array, it might optimize it to "multiply array length by element size" which will result in "almost compile time" sizeof. There is no thing called "array" in run time and thus there is nowhere to store its size.
@Shahbaz but if we know at compile time that int array[n]; length = sizeof(array) / sizeof(*array); we can assume that length = n; and avoid any computations at all.
Replacing sizeof(tbl[0]) by sizeof(char) is not an optimization -- nor is it correct. tbl[0] is a char*, not a char (and sizeof(char) is 1 by definition), so your "optimized" version simply yields the size in bytes of the array. sizeof is always evaluated at compile time unless the argument is a variable-length array.
|
19

The shorter and, arguably, cleaner version would look as

sizeof tbl / sizeof *tbl

:)

Comments

4

Yes, it will give you the number of elements in the array tb1.

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

Interpretation:

sizeof(tb1) will gives the size of the entire array i.e, tb1 = 3 bytes

sizeof(tb1[0]) gives the size of the character as tb1[0] gives a character value(value at address tb1+0) = 1 byte

Division of those two will give you 3 elements

7 Comments

tbl is an array of char*, not an array of char. If sizeof (char*) == 4, then you're dividing 12 bytes by 4 bytes, yield 3 array elements, not 3 bytes.
sorry its a typoo, it should be 3 elements not bytes..:)
@tbl still isn't a character array.
No, still incorrect. tbl is an array of char*, not an array of char. The size of tbl[0] isn't 1 byte unless you're on a weird system with 1-byte pointers.
but sizeof tb1[0] gives us the size of the value present at address tb1[0] which must be a character right?...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.