7
const char *array[] = {"ax","bo","cf"};

tried

printf("size of array = %lu\n", sizeof(const char*));

result != 3

also

printf("size of array = %lu\n", sizeof(array));
result != **DESIRED ANSWER** = 4

NOTE... I have read related questions on here but none had a relation with my question......

5
  • 1
    Curious: What makes you think sizeof(const char*) is 3? Depending on your architecture, if it is 32-bit it will return 4, 64-bit it will return 8. sizeof(array) will return 3*sizeof(pointer). Why do you think it should return 4? Commented Feb 9, 2017 at 4:38
  • 1
    Note that the size of a qualified pointer (const, volatile) is the same as the size of a non-qualified pointer. Commented Feb 9, 2017 at 4:47
  • @JonathanLeffler beware, on particular platforms, and depending on the (maybe non-standard) compiler, that could not be true. A const qualifier can make a pointer point to text (eprom), so using a different access than normal ram and sometimes a different size of the pointer. Commented Feb 9, 2017 at 7:08
  • 1
    The C standard says otherwise. There's no controlling what a non-standard compiler does, of course, but the C standard says (§6.2.5 Types) ¶26 Any type so far mentioned is an unqualified type. Each unqualified type has several qualified versions of its type, corresponding to the combinations of one, two, or all three of the const, volatile, and restrict qualifiers. The qualified or unqualified versions of a type are distinct types that belong to the same type category and have the same representation and alignment requirements. Commented Feb 9, 2017 at 7:12
  • 1
    Footnote 48 referenced at the end of the previous quote says: The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions. Commented Feb 9, 2017 at 7:12

3 Answers 3

10

To get the size of a const char pointer:`

printf("%zu\n", sizeof(const char *));

To get the size of the array array[]:

const char *array[] = {"ax","bo","cf"};
printf("%zu\n", sizeof array);

To get the number of elements in the array array[], divide the size of the array by the size of an array element.

const char *array[] = {"ax","bo","cf"};
// Size of array/size of array element
printf("%zu\n", sizeof array / sizeof array[0]); 
// expect 3
Sign up to request clarification or add additional context in comments.

3 Comments

Just out of curiosity, which is clearer? sizeof array / sizeof array[0] or sizeof array / sizeof *array. BTW I think the third example has typo. Should be array instead of a.
@RoadRunner Which is best sizeof array / sizeof array[0] or sizeof array / sizeof *array is easy. Every group should employ a coding style that settles style issues like this and then use the agreed upon one. I prefer [] at it emphasizes the array nature of the object.
@RoadRunner I avoid the sizeof array/sizeof(const char*) style as that is harder to code, review and maintain. To know the type is correct requires cross checking with the declaration. This is especial a nuisance when the type is defined in a field of some structure located in a header file.
3
anyType array[3] = {};    

sizeof(array[0]) //will return the size of one element of any array

sizeof(array) //will return the storage requirement of the entire array

(sizeof(array)/sizeof(array[0])) //will return the number of elements in the array

But remember that sizeof() resolves at compile time so it only works if you have the actual array variable, not a pointer to an element. Meaning you can't pass the array into a function and then take the sizeof() of the passed parameter unless you declare the parameter to be an array of fixed size.

2 Comments

"sizeof() resolves at compile time " --> sizeof also applies to variable length arrays, a run-time determination.
Good point on variable arrays, There is also the issue of an expression with side effects as parameter to sizeof which must be evaluated. But this is such a bad practice I don't think I have ever seen it in the wild.
1

you need to first find out the size of const char * then you need to divided by this to size of you array like this :

#include <stdio.h>
#include<string.h>

int main()
{
    const char *array[] = {"ax","bo","cf"};
    printf("%d",sizeof(array)/sizeof(const char*));
    return 0;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.