不要将 sizeof 用于 C 中的数组参数
直接使用sizeof来查找数组的大小可能会导致代码出错,因为数组参数被视为指针。考虑下面的程序。
C
// C Program to demonstrate incorrect usage of sizeof() for
// arrays
#include
void fun(int arr[])
{
int i;
// sizeof should not be used here to get number
// of elements in array
int arr_size = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < arr_size; i++) {
arr[i] = i;
}
// executed only once
}
// Driver Code
int main()
{
int i;
int arr[4] = { 0, 0, 0, 0 };
fun(arr);
// use of sizeof is fine here
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
printf(" %d ", arr[i]);
getchar();
return 0;
}
C
// C Program to demonstrate correct usage of sizeof() for
// arrays
#include
void fun(int arr[], size_t arr_size)
{
int i;
for (i = 0; i < arr_size; i++) {
arr[i] = i;
}
}
// Driver Code
int main()
{
int i;
int arr[] = { 0, 0, 0, 0 };
size_t n = sizeof(arr) / sizeof(arr[0]);
fun(arr, n);
printf("The size of the array is: %ld", n);
printf("\nThe elements are:\n");
for (i = 0; i < n; i++)
printf(" %d ", arr[i]);
getchar();
return 0;
}
C
// C Program to demonstrate usage of macros to find the size
// of arrays
#include
#define SIZEOF(arr) sizeof(arr) / sizeof(*arr)
void fun(int arr[], size_t arr_size)
{
int i;
for (i = 0; i < arr_size; i++) {
arr[i] = i;
}
}
// Driver Code
int main()
{
int i;
int arr[] = { 0, 0, 0, 0, 0 };
size_t n = SIZEOF(arr);
fun(arr, n);
printf("The size of the array is: %ld", n);
printf("\nThe elements are:\n");
for (i = 0; i < n; i++)
printf(" %d ", arr[i]);
return 0;
}
C
// C Program to demonstrate usage of pointer arithmetic to
// find the size of arrays
#include
void fun(int arr[], size_t arr_size)
{
int i;
for (i = 0; i < arr_size; i++) {
arr[i] = i;
}
}
// Driver Code
int main()
{
int i;
int arr[] = { 0, 0, 0, 0, 0 };
size_t n = (&arr)[1] - arr;
fun(arr, n);
printf("The size of the array is: %ld", n);
printf("\nThe elements are:\n");
for (i = 0; i < n; i++)
printf(" %d ", arr[i]);
return 0;
}
说明:此代码生成错误,因为函数fun() 接收数组参数 'arr[]' 并尝试使用sizeof运算符找出 arr[] 中的元素数。
在 C 中,数组参数被视为指针(有关详细信息,请参阅this )。因此,表达式sizeof(arr)/sizeof(arr[0])变为sizeof(int *)/sizeof(int) ,结果为 1(int 和 int * 的大小为 4),而 fun() 中的 for 循环为只执行一次,与数组大小无关。因此,在这种情况下,不应使用sizeof来获取多个元素。
解决方案:
1) 使用单独的参数:数据类型size_t的单独参数用于数组大小或长度应传递给 fun()。 size_t是至少 16 位的无符号整数类型。因此,更正后的程序将是:
C
// C Program to demonstrate correct usage of sizeof() for
// arrays
#include
void fun(int arr[], size_t arr_size)
{
int i;
for (i = 0; i < arr_size; i++) {
arr[i] = i;
}
}
// Driver Code
int main()
{
int i;
int arr[] = { 0, 0, 0, 0 };
size_t n = sizeof(arr) / sizeof(arr[0]);
fun(arr, n);
printf("The size of the array is: %ld", n);
printf("\nThe elements are:\n");
for (i = 0; i < n; i++)
printf(" %d ", arr[i]);
getchar();
return 0;
}
输出
The size of the array is: 4
The elements are:
0 1 2 3
2)使用宏:我们甚至可以使用#define定义宏来查找数组的大小,如下所示,
C
// C Program to demonstrate usage of macros to find the size
// of arrays
#include
#define SIZEOF(arr) sizeof(arr) / sizeof(*arr)
void fun(int arr[], size_t arr_size)
{
int i;
for (i = 0; i < arr_size; i++) {
arr[i] = i;
}
}
// Driver Code
int main()
{
int i;
int arr[] = { 0, 0, 0, 0, 0 };
size_t n = SIZEOF(arr);
fun(arr, n);
printf("The size of the array is: %ld", n);
printf("\nThe elements are:\n");
for (i = 0; i < n; i++)
printf(" %d ", arr[i]);
return 0;
}
输出
The size of the array is: 5
The elements are:
0 1 2 3 4
3) 使用指针算法:我们可以使用(&arr)[1] – arr来求数组的大小。这里, arr指向数组的第一个元素,类型为int*。而且, &arr的类型为int*[n]并指向整个数组。因此它们的差异相当于数组的大小。
C
// C Program to demonstrate usage of pointer arithmetic to
// find the size of arrays
#include
void fun(int arr[], size_t arr_size)
{
int i;
for (i = 0; i < arr_size; i++) {
arr[i] = i;
}
}
// Driver Code
int main()
{
int i;
int arr[] = { 0, 0, 0, 0, 0 };
size_t n = (&arr)[1] - arr;
fun(arr, n);
printf("The size of the array is: %ld", n);
printf("\nThe elements are:\n");
for (i = 0; i < n; i++)
printf(" %d ", arr[i]);
return 0;
}
输出
The size of the array is: 5
The elements are:
0 1 2 3 4