C语言 C 传递数组的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5493281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:14:02  来源:igfitidea点击:

C sizeof a passed array

carraysfunctionsizeof

提问by Untamed

Possible Duplicate:
How to find the sizeof( a pointer pointing to an array )

可能的重复:
如何找到 sizeof(指向数组的指针)

I understand that the sizeof operator is evaluated and replaced with a constant at compile time. Given that, how can a function, being passed different arrays at different points in a program, have it's size computed? I can pass it as a parameter to the function, but I'd rather not have to add another parameter if I don't absolutely have to.

我知道 sizeof 运算符在编译时被评估并替换为常量。鉴于此,在程序的不同点传递不同数组的函数如何计算其大小?我可以将它作为参数传递给函数,但如果不是绝对必须,我宁愿不必添加另一个参数。

Here's an example to illustrate what I'm asking:

这是一个示例来说明我的要求:

#include <stdio.h>
#include <stdlib.h>

#define SIZEOF(a) ( sizeof a / sizeof a[0] )


void printarray( double x[], int );

int main()
{
        double array1[ 100 ];


        printf( "The size of array1 = %ld.\n", SIZEOF( array1 ));
        printf( "The size of array1 = %ld.\n", sizeof array1 );
        printf( "The size of array1[0] = %ld.\n\n", sizeof array1[0] );

        printarray( array1, SIZEOF( array1 ) );

        return EXIT_SUCCESS;
}


void printarray( double p[], int s )
{
        int i;


        // THIS IS WHAT DOESN"T WORK, SO AS A CONSEQUENCE, I PASS THE 
        // SIZE IN AS A SECOND PARAMETER, WHICH I'D RATHER NOT DO.
        printf( "The size of p calculated = %ld.\n", SIZEOF( p ));
        printf( "The size of p = %ld.\n", sizeof p );
        printf( "The size of p[0] = %ld.\n", sizeof p[0] );

        for( i = 0; i < s; i++ )
                printf( "Eelement %d = %lf.\n", i, p[i] );

        return;
}

采纳答案by DigitalRoss

There is no magic solution. C is not a reflective language. Objects don't automatically know what they are.

没有神奇的解决方案。C 不是反射式语言。对象不会自动知道它们是什么。

But you have many choices:

但是你有很多选择:

  1. Obviously, add a parameter
  2. Wrap the call in a macro and automatically add a parameter
  3. Use a more complex object. Define a structure which contains the dynamic array and also the size of the array. Then, pass the address of the structure.
  1. 显然,添加一个参数
  2. 将调用包装在宏中并自动添加参数
  3. 使用更复杂的对象。定义一个包含动态数组和数组大小的结构。然后,传递结构的地址。

回答by aschepler

Function parameters never actually have array type. When the compiler sees

函数参数实际上从来没有数组类型。当编译器看到

void printarray( double p[], int s )

or even

甚至

void printarray( double p[100], int s )

it converts either one to

它将任一转换为

void printarray( double* p, int s )

So sizeof(p)is sizeof(double*). And yes, you'll have to pass the size as a parameter.

所以sizeof(p)sizeof(double*)。是的,您必须将大小作为参数传递。

回答by John Bode

The problem is that your function doesn't receive an array value; it receives a pointer value.

问题是您的函数没有收到数组值;它接收一个指针值。

Except when it is the operand of the sizeofor unary &operators, or it is a string literal being used to initialize another array in a declaration, an expression of type "array of T" will be converted to type "pointer to T" and its value will be the address of the first element of the array.

除非它是sizeof或 一元运算&符的操作数,或者是用于在声明中初始化另一个数组的字符串文字,否则“数组T”类型的表达式将转换为“指向T”类型的表达式,其值将是数组第一个元素的地址。

Thus, when you call printarray, the type of array1is implicitly converted from "100-element array of double" to "pointer to double." Thus, the type of the parameter pis double *, not double [100].

因此,当您调用 时printarray, 的类型array1会从“100 个元素的数组double”隐式转换为“指向 的指针double”。因此,参数的类型pdouble *,不是double [100]

In the context of a function parameter declaration, T a[]is identical to T *a.

在函数参数声明的上下文中,T a[]T *a.

This is why you have to pass the array size separately;

这就是为什么您必须单独传递数组大小的原因;

回答by bobDevil

You answered your own question. It's computed at compile-time, so how can 'sizeof p' possibly have more than one value during runtime?

你是在自问自答。它是在编译时计算的,那么“sizeof p”怎么可能在运行时有多个值呢?

Passing the length as a parameter is a fine solution, otherwise you can make sure your arrays always end with some special value (like strings and the null byte).

将长度作为参数传递是一个很好的解决方案,否则您可以确保您的数组始终以某些特殊值(如字符串和空字节)结尾。

回答by Scott Duckworth

You must either

你必须要么

  1. Pass the size of the array as a parameter to the function
  2. Ensure that the array ends with a known value, and stop when you reach that value.
  1. 将数组的大小作为参数传递给函数
  2. 确保数组以已知值结束,并在达到该值时停止。

回答by Eric

If your array is null terminated then you can iterate through the array and find the last value. Many string operators in C work that way

如果您的数组以空值终止,那么您可以遍历数组并找到最后一个值。C 中的许多字符串运算符都是这样工作的

Otherwise you don't have much choice.

否则你没有太多选择。

sizeof will return the size of p, which is a pointer and will equals to your int size

sizeof 将返回 p 的大小,它是一个指针,将等于您的 int 大小

回答by Robert S. Barnes

You can use a sentinel value which is the way strings are dealt with. The drawback is that you have to iterate over the entire array to find it's size.

您可以使用标记值,这是处理字符串的方式。缺点是您必须遍历整个数组才能找到它的大小。

For example, when you have a string such as:

例如,当您有一个字符串时,例如:

char s1[] = "foobar";

What you actually have is an array of length 7, with the seventh spot being a null terminating char '\0' which serves to indicate the end of the array / string.

您实际拥有的是一个长度为 7 的数组,第七个点是一个空终止字符 '\0' ,用于指示数组/字符串的结尾。

Another way you could do it is to create a struct which consists of a size followed by the array.

另一种方法是创建一个由大小和数组组成的结构。