在C语言中,有没有一种方法可以创建一个函数来获取数组的大小?[复制]

oxf4rvwz  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(59)

此问题在此处已有答案

How do you get the size of array that is passed into the function?(8个答案)
How to get array size within function?(1个答案)
Calculate Length of Array in C by Using Function(9个答案)
昨天就关门了。
如何在C语言中创建一个函数来获取数组的大小?函数和数组不在同一个作用域。例如:

int func(int arr[]);

int main(){
    int arr[5];
}

个字符
上述函数的问题是,它将arr视为指针,因此它具有指针的大小

f1tvaqid

f1tvaqid1#

简单地将数组长度沿着传递给参数中的数组指针,这是很常见的

int some_func(int arr[], int n);

字符串
或者在应用程序中为数组创建更复杂的结构:

typedef struct {
  int *vector;
  int length;
  int capacity;
} Array;


你也可以使用我的实现:https://gist.github.com/arianito/57e668790b723a6e6649d4535e9dcaae

相关问题