c - Length of an array using sizeof -
i trying determine length of array using sizeof function.
my code looks this:
int n; n = sizeof(a)/sizeof(a[0]); i keep getting warning saying type inconsistent. understand how fix problem?
the error telling a function parameter, , sizeof(a) return size of int* instead of actual size of array. reason because function parameters of array types same thing pointers (so parameter of type int[] parameter of type int*) , size specified in array type information reader, not compiler enforces.
the sizeof(a)/sizeof(a[0]) trick suitable use macros, not should writing in non-macro code, because if you're in context it's possible statically know length of array (e.g. didn't come function parameter) should know without having compute it.
Comments
Post a Comment