How to get the length of an array of C pointers?

Possible duplicate:
array length in function argument

Is there any method like Java maybe .length from an array of C points? Thanks.

+5
source share
3 answers

No, with the C pointer, you cannot determine the length in an agnostic platform.

For a real C array, though see dirkgently answer

+6
source

You can get it with a macro:

#define sizeofa(array) sizeof array / sizeof array[ 0 ] 

if and only if the array is automatic and you access it within your definition as:

 #include <stdio.h> int main() { int x[] = { 1, 2, 3 }; printf("%zd\n", sizeofa( x )); return 0; } 

However, if you only have a (decomposed) pointer, you cannot get the length of the array without resorting to some kind of intolerable hacking implementation.

+3
source

If you are using MSVC / MinGW, for a real C pointer, there is a NON-PORT solution:

 #include <malloc.h> char *s=malloc(1234); #ifdef __int64 printf( "%lu\n", _msize(s)); #else #endif 

For a real C array like

 AnyType myarray[] = {...}; 

or

 AnyType myarray[constVal]; 

see another answer.

+1
source

Source: https://habr.com/ru/post/969659/


All Articles