在功能参数数组长度 [英] Length of array in function argument

查看:128
本文介绍了在功能参数数组长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是众所周知的code计算C中的数组长度:

This is well known code to compute array length in C:

sizeof(array)/sizeof(type)

但我似乎无法找出作为参数传递给函数的数组的长度:

But I can't seem to find out the length of the array passed as an argument to a function:

#include <stdio.h>

int length(const char* array[]) {
  return sizeof(array)/sizeof(char*);
}

int main() {
  const char* friends[] = { "John", "Jack", "Jim" };
  printf("%d %d", sizeof(friends)/sizeof(char*), length(friends)); // 3 1
}

我假设数组由值复制到函数的参数作为常量指针和借鉴它应该解决这个问题,但是这个声明是无效的:

I assume that array is copied by value to the function argument as constant pointer and reference to it should solve this, but this declaration is not valid:

int length(const char**& array);

我觉得传递数组长度为第二个参数是多余的信息,但为什么是标准的声明是这样的:

int main(int argc, char** argv);

请解释一下,如果有可能找出函数参数数组的长度,如果是这样,为什么会出现在冗余

Please explain if it is possible to find out the array length in function argument, and if so, why is there the redundancy in main.

推荐答案

的sizeof 只能找到数组的长度,如果你将它应用到原始数组。

sizeof only works to find the length of the array if you apply it to the original array.

int a[5]; //real array. NOT a pointer
sizeof(a); // :)

然而,由阵列衰变为指针的时候,sizeof的会给指针,而不是阵列的大小。

However, by the time the array decays into a pointer, sizeof will give the size of the pointer and not of the array.

int a[5];
int * p = a;
sizeof(p); // :(

正如你已经巧妙地指出,主要接收数组的长度作为一个参数(ARGC)。的是的,这是出于需要,而不是冗余的。 (嗯,这是一种reduntant,因为argv的便利,以空指针终止,但我离题)

As you have already smartly pointed out main receives the length of the array as an argument (argc). Yes, this is out of necessity and is not redundant. (Well, it is kind of reduntant since argv is conveniently terminated by a null pointer but I digress)

有一些理由,为什么这会发生。我们怎样才能使事情使C数组也知道它的长度?

There is some reasoning as to why this would take place. How could we make things so that a C array also knows its length?

第一个想法是没有数组衰变成指针时它们传递给一个函数,并继续保持数组长度的类型系统。关于这个坏事是,你需要对每一个可能的数组长度一个单独的功能,这样做是不是一个好主意。 (帕斯卡这样做,有些人认为这是它的丢失到C的原因之一)

A first idea would be not having arrays decaying into pointers when they are passed to a function and continuing to keep the array length in the type system. The bad thing about this is that you would need to have a separate function for every possible array length and doing so is not a good idea. (Pascal did this and some people think this is one of the reasons it "lost" to C)

第二个想法是存储旁边的数组数组的长度,就像任何现代编程语言所做的:

A second idea is storing the array length next to the array, just like any modern programming language does:

a -> [5];[0,0,0,0,0]

但你只是建立一个无形的结构幕后以及C哲学并不认可这种开销的。这就是说,建立这样一个自己的结构往往是一些各种各样的问题,是一个好主意:

But then you are just creating an invisible struct behind the scenes and the C philosophy does not approve of this kind of overhead. That said, creating such a struct yourself is often a good idea for some sorts of problems:

struct {
    size_t length;
    int * elements;
}


你可以想想另一件事是如何在C字符串是空终止存储长度(如帕斯卡)代替。要存储的长度,而无需担心限额需要的高达的四个字节,一个难以想像的昂贵的量(至少当时)。有人可能会想,如果阵列可以也为空终止这样的,但那么你将如何让数组存储空?


Another thing you can think about is how strings in C are null terminated instead of storing a length (as in Pascal). To store a length without worrying about limits need a whopping four bytes, an unimaginably expensive amount (at least back then). One could wonder if arrays could be also null terminated like that but then how would you allow the array to store a null?

这篇关于在功能参数数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆