It does not work even it is not from a function. For example, int* arr=new int[10]; cout << sizeof(arr)/sizeof(arr[0]) << endl; You will get 1. You are right, size should be sent as a parameter.
【在 c**********e 的大作中提到】 : It does not work even it is not from a function. : For example, : int* arr=new int[10]; : cout << sizeof(arr)/sizeof(arr[0]) << endl; : You will get 1. : You are right, size should be sent as a parameter.
g*e
21 楼
i don't think there's a way to get the size if not explicitly given.
c*e
22 楼
This only works for c-string.
【在 c**********e 的大作中提到】 : It does not work even it is not from a function. : For example, : int* arr=new int[10]; : cout << sizeof(arr)/sizeof(arr[0]) << endl; : You will get 1. : You are right, size should be sent as a parameter.
c*e
23 楼
For array on stack, it works. int arr[100]; cout << sizeof(arr)/sizeof(int) << endl; You do get 100. Though I am not sure if it is system dependent.
【在 g*********e 的大作中提到】 : i don't think there's a way to get the size if not explicitly given.
c*e
24 楼
This only works for c-string.
【在 r********g 的大作中提到】 : 可以遍历一遍,到了“NULL”就停止。。。
h*f
25 楼
as others mentioned, there is no way unless the array is on the stack. that's why you see a lot of function declaration like void foo(int* array, size_t length).
【在 c**********e 的大作中提到】 : It does not work even it is not from a function. : For example, : int* arr=new int[10]; : cout << sizeof(arr)/sizeof(arr[0]) << endl; : You will get 1. : You are right, size should be sent as a parameter.