如何在不声明C语言大小的情况下创建数组? [英] How to create an array without declaring the size in C?

查看:135
本文介绍了如何在不声明C语言大小的情况下创建数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对新C语言还很陌生,因此找不到任何与此相关的信息(也许是因为我不确定自己在寻找什么).

I'm pretty new to new C and I wasn't able to find anything related to this (maybe because I'm not really sure what I'm looking for).

我正在尝试创建一个没有大小的int和float数组(它可能为0或在用户使用该程序时可能会增加).

I'm trying to create a int and a float array without a size (it might be 0 or it might increment while the user use the program).

我正在尝试执行以下操作:

I was trying to do the follow:

int bills[];

float totalAmount[];

我无法分配最大大小,因为我正在用for循环打印每个数组(如果我分配的大小为99,我将打印99行,但我不希望这样做).

I can't assign a max size because I'm printing each array with a for loop (If I assign a size of 99 I'll print 99 lines, and I don't want that).

我很确定这是可能的,但我不知道怎么做.

I'm pretty sure that this is possible but I don't know how.

谢谢!

推荐答案

C不支持具有动态元素数量的数组.数组的元素数必须在编译时确定,或者因为可以在创建时在运行时评估C99.创建阵列后,其大小是固定的,无法更改.在某些情况下,无论是在数组定义中还是在数组声明中,都没有在[]之间明确指定大小.

C does not support arrays with a dynamic number of elements. The number of elements of an array must be determined either at compile time or since C99 can be evaluated at runtime at the point of creation. Once the array is created, its size is fixed and cannot be changed. There are a few cases where the size is not explicitly specified between the [], either in array definitions or in array declarations.

如果提供了初始化程序,则可以定义一个没有最左边尺寸的数组.编译器将从初始值设定项推断出大小:

You can define an array without an explicit size for the leftmost dimension if you provide an initializer. The compiler will infer the size from the initializer:

int a[] = { 1, 2, 3 };              // equivalent to int a[3] = { 1, 2, 3 };
int m[][2] = {{ 1, 2 }, { 3, 4 }};  // equivalent to int m[2][2] = {{ 1, 2 }, { 3, 4 }};
char s[] = "Hello world\n";         // equivalent to char s[13] = "Hello world\n";

请注意,编译器如何在字符串的情况下添加隐式null终止符.

Note how the compiler adds the implicit null terminator in the string case.

在多种情况下,您可以声明一个没有尺寸说明符的最左侧尺寸的数组:

You can declare an array without a size specifier for the leftmost dimension in multiples cases:

  • 作为具有extern类存储的全局变量(该数组在其他位置定义),
  • 作为功能参数:int main(int argc, char *argv[]).在这种情况下,始终会忽略为最左侧尺寸指定的尺寸.
  • 作为struct的最后一个成员.这是一个称为 flexible array 的C99扩展.
  • as a global variable with extern class storage (the array is defined elsewhere),
  • as a function parameter: int main(int argc, char *argv[]). In this case the size specified for the leftmost dimension is ignored anyway.
  • as the last member of a struct. This is a C99 extension called a flexible array.

编译器没有有关这些数组的实际大小的信息.程序员使用其他信息从单独的变量或数组内容中确定长度.

The compiler has no information on the actual size of these arrays. The programmer with use some other information to determine the length, either from a separate variable or from the array contents.

在使用函数参数的情况下,数组作为指针传递,即使指定了元素数量,sizeof(argv)也会求出指针的大小.

In the case of a function argument, the array is passed as a pointer and even if the number of elements is specified, sizeof(argv) evaluates to the size of a pointer.

这篇关于如何在不声明C语言大小的情况下创建数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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