How to find the size of an integer array in C.
Any method available without traversing the whole array once, to find out the size of the array.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the array is a global, static, or automatic variable (
int array[10];), thensizeof(array)/sizeof(array[0])works.If it is a dynamically allocated array (
int* array = malloc(sizeof(int)*10);) or passed as a function argument (void f(int array[])), then you cannot find its size at run-time. You will have to store the size somewhere.Note that
sizeof(array)/sizeof(array[0])compiles just fine even for the second case, but it will silently produce the wrong result.