C 传递数组的大小

发布于 2024-10-28 04:00:25 字数 1312 浏览 5 评论 0原文

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

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

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

#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;
}

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

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.

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

九公里浅绿 2024-11-04 04:00:25

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

但你有很多选择:

  1. 显然,添加一个参数
  2. 将调用包装在宏中并自动添加一个参数
  3. 使用更复杂的对象。定义一个包含动态数组以及数组大小的结构。然后,传递结构的地址。

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

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.
我的影子我的梦 2024-11-04 04:00:25

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

void printarray( double p[], int s )

或者甚至

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

将其中之一转换为时,

void printarray( double* p, int s )

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

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.

若沐 2024-11-04 04:00:25

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

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

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

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

这就是为什么你必须单独传递数组大小;

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 sizeof or 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.

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

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

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

女皇必胜 2024-11-04 04:00:25

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

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

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

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).

清引 2024-11-04 04:00:25

您必须

  1. 将数组的大小作为参数传递给函数
  2. 确保数组以已知值结束,并在达到该值时停止。

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.
鱼忆七猫命九 2024-11-04 04:00:25

如果您的数组以 null 结尾,那么您可以迭代该数组并找到最后一个值。 C 中的许多字符串运算符都是这样工作的,

否则你没有太多选择。

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

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

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

桜花祭 2024-11-04 04:00:25

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

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

char s1[] = "foobar";

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

另一种方法是创建一个结构体,其中包含一个大小,后跟数组。

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文