C sizeof char* array

后端 未结 3 1751
温柔的废话
温柔的废话 2020-12-14 00:33

I have a char* array as follows:

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

How do I use the sizeof operator to get t

3条回答
  •  执笔经年
    2020-12-14 01:38

    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

提交回复
热议问题