在 C 中使用 sizeof 函数
Using sizeof function in c
我想用 c 编写一个函数,它能够打印我们创建的数组。但是,有一个问题。当我想创建循环时,我需要知道数组中元素的数量。所以,我使用了 sizeof 函数来解决这个问题。
我写了size = sizeof(list) / sizeof(list[0]);
行是为了得到整个数组的大小,然后把它除以一个元素的大小,从而得到数组中元素的数量。但是,当我构建代码时,我收到以下警告:
warning : 'sizeof' on array function parameter 'list' will return the size of 'int *'
#include <stdio.h>
void displayArray(int myarray[]);
int main()
{
int list1[] = {0, 5, 3, 5, 7, 9, 5};
displayArray(list1);
return 0;
}
void displayArray(int list[])
{
int size;
size = sizeof(list) / sizeof(list[0]);
printf("Your array is: ");
for (int i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");
return;
}
void displayArray(int list[])
与 void displayArray(int *list)
完全 相同。这是因为数组本身没有值 ,当您尝试仅计算数组的标识符时,您得到的是指向其第一个元素的指针。
因此,将数组传递给函数始终意味着传递指针。 sizeof
operator(不是函数)获取的不是你的数组类型而是指针类型,指针的大小对你没用
一种典型的方法是将数组的大小作为第二个参数显式传递给您的函数,正确的类型是 size_t
。
没有数组参数这样的东西。它们只是隐藏的指针。
你应该在调用者中调整你的数组大小(那里有一个真正的数组)
并将大小传递给被调用者。
(当数组传递给被调用者时,它们的大小信息在数组到指针的转换中得到很多,所以你必须单独传递它)
#include <stdio.h>
void displayArray(int myarray[], int size);
int main()
{
int list1[] = {0, 5, 3, 5, 7, 9, 5};
displayArray(list1, sizeof list1 / sizeof list1[0]);
/*you don't need the parens if the argument is a variable / isn't a type */
return 0;
}
void displayArray(int list[], int size)
{
printf("Your array is: ");
for (int i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");
return;
}
您应该已经创建了一个变量来包含数组的大小,并将其作为参数添加到函数中。 'list' 只是指向数组第一个元素的指针,因此当您检查 sizeof(list)
时,它 returns 指针的大小。
像这样尝试:
#include <stdio.h>
void displayArray(int myarray[]);
int main()
{
int list1[] = {0, 5, 3, 5, 7, 9, 5};
int size1 = 7;
displayArray(list1,size1);
return 0;
}
void displayArray(int list[], int size)
{
printf("Your array is: ");
for (int i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");
return;
}
根据 C 标准(6.3.2.1 左值、数组和函数指示符)
3 Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ‘‘array of type’’ is converted to an
expression with type ‘‘pointer to type’’ that points to the initial
element of the array object and is not an lvalue. If the array object
has register storage class, the behavior is undefined.
And (6.7.6.3 Function declarators (including prototype))
7 A declaration of a parameter as ‘‘array of type’’ shall be
adjusted to ‘‘qualified pointer to type’’, where the type qualifiers
(if any) are those specified within the [ and ] of the array type
derivation...
因此下面的函数声明声明了同一个函数
void displayArray(int myarray[100]);
void displayArray(int myarray[10]);
void displayArray(int myarray[1]);
void displayArray(int myarray[]);
void displayArray(int *myarray);
您可以在您的程序中包含所有这些声明(不是定义)。声明数组的函数参数调整为指针类型
因此,如果数组没有标记值,则需要将数组的大小显式传递给函数。例如
void displayArray( const int *myarray, size_t n );
//...
displayArray( list1, sizeof( list1 ) / sizeof( *list1 ) );
我想用 c 编写一个函数,它能够打印我们创建的数组。但是,有一个问题。当我想创建循环时,我需要知道数组中元素的数量。所以,我使用了 sizeof 函数来解决这个问题。
我写了size = sizeof(list) / sizeof(list[0]);
行是为了得到整个数组的大小,然后把它除以一个元素的大小,从而得到数组中元素的数量。但是,当我构建代码时,我收到以下警告:
warning : 'sizeof' on array function parameter 'list' will return the size of 'int *'
#include <stdio.h>
void displayArray(int myarray[]);
int main()
{
int list1[] = {0, 5, 3, 5, 7, 9, 5};
displayArray(list1);
return 0;
}
void displayArray(int list[])
{
int size;
size = sizeof(list) / sizeof(list[0]);
printf("Your array is: ");
for (int i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");
return;
}
void displayArray(int list[])
与 void displayArray(int *list)
完全 相同。这是因为数组本身没有值 ,当您尝试仅计算数组的标识符时,您得到的是指向其第一个元素的指针。
因此,将数组传递给函数始终意味着传递指针。 sizeof
operator(不是函数)获取的不是你的数组类型而是指针类型,指针的大小对你没用
一种典型的方法是将数组的大小作为第二个参数显式传递给您的函数,正确的类型是 size_t
。
没有数组参数这样的东西。它们只是隐藏的指针。
你应该在调用者中调整你的数组大小(那里有一个真正的数组) 并将大小传递给被调用者。
(当数组传递给被调用者时,它们的大小信息在数组到指针的转换中得到很多,所以你必须单独传递它)
#include <stdio.h>
void displayArray(int myarray[], int size);
int main()
{
int list1[] = {0, 5, 3, 5, 7, 9, 5};
displayArray(list1, sizeof list1 / sizeof list1[0]);
/*you don't need the parens if the argument is a variable / isn't a type */
return 0;
}
void displayArray(int list[], int size)
{
printf("Your array is: ");
for (int i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");
return;
}
您应该已经创建了一个变量来包含数组的大小,并将其作为参数添加到函数中。 'list' 只是指向数组第一个元素的指针,因此当您检查 sizeof(list)
时,它 returns 指针的大小。
像这样尝试:
#include <stdio.h>
void displayArray(int myarray[]);
int main()
{
int list1[] = {0, 5, 3, 5, 7, 9, 5};
int size1 = 7;
displayArray(list1,size1);
return 0;
}
void displayArray(int list[], int size)
{
printf("Your array is: ");
for (int i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");
return;
}
根据 C 标准(6.3.2.1 左值、数组和函数指示符)
3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
And (6.7.6.3 Function declarators (including prototype))
7 A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation...
因此下面的函数声明声明了同一个函数
void displayArray(int myarray[100]);
void displayArray(int myarray[10]);
void displayArray(int myarray[1]);
void displayArray(int myarray[]);
void displayArray(int *myarray);
您可以在您的程序中包含所有这些声明(不是定义)。声明数组的函数参数调整为指针类型
因此,如果数组没有标记值,则需要将数组的大小显式传递给函数。例如
void displayArray( const int *myarray, size_t n );
//...
displayArray( list1, sizeof( list1 ) / sizeof( *list1 ) );