C语言 用c语言声明没有初始大小的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17381867/
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
Declaring arrays in c language without initial size
提问by ViSTA
Write a program to manipulate the temperature details as given below.
- Input the number of days to be calculated. – Main function
- Input temperature in Celsius – input function
- Convert the temperature from Celsius to Fahrenheit.- Separate function
- find the average temperature in Fahrenheit.
编写一个程序来操作温度细节,如下所示。
- 输入要计算的天数。– 主要功能
– 以摄氏度为单位输入温度 – 输入功能
– 将温度从摄氏温度转换为华氏度。- 独立功能
–以华氏度为单位查找平均温度。
how can I make this program without initial size of array ??
我怎样才能在没有数组初始大小的情况下制作这个程序?
#include<stdio.h>
#include<conio.h>
void input(int);
int temp[10];
int d;
void main()
{
int x=0;
float avg=0,t=0;
printf("\nHow many days : ");
scanf("%d",&d);
input(d);
conv();
for(x=0;x<d;x++)
{
t=t+temp[x];
}
avg=t/d;
printf("Avarage is %f",avg);
getch();
}
void input(int d)
{
int x=0;
for(x=0;x<d;x++)
{
printf("Input temperature in Celsius for #%d day",x+1);
scanf("%d",&temp[x]);
}
}
void conv()
{
int x=0;
for(x=0;x<d;x++)
{
temp[x]=1.8*temp[x]+32;
}
}
回答by Hogan
In C arrays and pointers are closely related. In fact, by design an array is just a syntax convention for accessing a pointer to an allocated memory. *(see note for more details below)
在 C 中数组和指针是密切相关的。事实上,按照设计,数组只是用于访问指向已分配内存的指针的语法约定。*(有关更多详细信息,请参阅下面的注释)
So in C the statement
所以在 C 中的语句
anyarray[n]
is the same as
是相同的
*(anyarray+n)
Using pointer arithmetic.
使用指针算法。
You don't really have to worry about the details to make it "work"as it is designed to be somewhat intuitive.
您真的不必担心细节以使其“工作”,因为它的设计有点直观。
Just create a pointer, and allocate the memory and then access it like as an array.
只需创建一个指针,分配内存,然后像数组一样访问它。
Here is some examples --
下面是一些例子——
int *temp = null; // this will be our array
// allocate space for 10 items
temp = malloc(sizeof(int)*10);
// reference the first element of temp
temp[0] = 70;
// free the memory when done
free(temp);
Remember -- if you access outside of the allocated area you will have unknown effects.
请记住——如果您在分配的区域之外访问,您将产生未知的影响。
- To be clear it is the indexing operator (
[ ]) that is translated to pointer arithmetic. This is not an array in the modern sense of the type. Whether (or not) the pointer involved points to (dynamically) allocated memory is inconsequential to how this operator works. In a more modern language you would be able to operate on the array as an abstract type (to see how big it is, for example), you can't do this in C.
- 需要明确的是,索引运算符 (
[ ]) 被转换为指针算术。这不是现代意义上的数组。所涉及的指针是否(或是否)指向(动态)分配的内存与该运算符的工作方式无关。在更现代的语言中,您可以将数组作为抽象类型进行操作(例如,查看它有多大),而在 C 中则无法执行此操作。
回答by btidwell
An array without an initial size is basically just a pointer. In order to dynamically set the size of the array, you need to use the malloc()or calloc()functions. These will allocate a specified amount of bytes of memory.
没有初始大小的数组基本上只是一个指针。为了动态设置数组的大小,需要使用malloc()orcalloc()函数。这些将分配指定数量的内存字节。
In your code above, declare tempas an int pointer
在上面的代码中,声明temp为 int指针
int *temp;
Then allocate space for it using malloc()or calloc(). The argument that these functions take is is the number of bytesof memory to allocate. In this case, you want enough space for dints. So...
然后使用malloc()or为其分配空间calloc()。这些函数采用的参数是要分配的内存字节数。在这种情况下,您需要足够的空间用于d整数。所以...
temp = malloc(d * sizeof(int));
mallocreturns a pointer to the first byte in the block of memory that was just allocated. Regular arrays are simply pointers to the first byte in a sectioned off block of memory, which is exactly what tempis now. Thus, you can treat the temppointer as an array! Like so:
malloc返回一个指向刚刚分配的内存块中第一个字节的指针。常规数组只是指向分区内存块中第一个字节的指针,这正是temp现在的情况。因此,您可以将temp指针视为数组!像这样:
temp[1] = 10;
int foo = temp[1];
printf("%d", foo);
Outputs
输出
10
回答by Drew McGowen
You will need to declare tempas an intpointer (instead of an intarray). Then, you can use mallocin your main(after your first scanf):
您需要声明temp为int指针(而不是int数组)。然后,您可以malloc在您的main(在您的第一个之后scanf)使用:
temp = malloc(d * sizeof(int));
回答by haccks
If your compiler supports c99, then simply use VLA(variable length array).Use like this:
如果您的编译器支持c99,那么只需使用VLA(可变长度数组)。使用如下:
void input(int);
int d;
void main()
{
int x=0;
float avg=0,t=0;
printf("\nHow many days : ");
scanf("%d",&d);
int temp[d];
input(d);
conv();
for(x=0;x<d;x++)
{
t=t+temp[x];
}
avg=t/d;
printf("Avarage is %f",avg);
getch();
}
Now temp[]is defined inside main()after date input.
现在在输入日期后在temp[]里面定义main()。
回答by codezombie
1-add #include<stdlib.h>at the top of your file. Then modify the conv() code as follows:
2- modify temp declaration as follows (global variable):
1-#include<stdlib.h>在文件顶部添加。然后修改conv()代码如下:
2-修改temp声明如下(全局变量):
int *temp;
3- modify input(int d)function as follows (tested on Visual Studio 2010):
3-修改input(int d)功能如下(在Visual Studio 2010上测试):
void input(int d)
{
int x=0;
temp=(int*)malloc(sizeof(int)*d);
for(x=0;x<d;x++)
{
printf("Input temperature in Celsius for #%d day",x+1);
scanf("%d",&temp[x]);
}
}
回答by Immueggpain
I didn't change anything else so you may see it clearly.
我没有更改任何其他内容,因此您可以清楚地看到它。
#include<stdio.h>
#include<conio.h>
#include <stdlib.h> //here
void input(int);
int *temp=0; //here
int d;
void main()
{
int x=0;
float avg=0,t=0;
printf("\nHow many days : ");
scanf("%d",&d);
temp=malloc(d * sizeof(int)); //here
input(d);
conv();
for(x=0;x<d;x++)
{
t=t+temp[x];
}
avg=t/d;
printf("Avarage is %f",avg);
getch();
}
void input(int d)
{
int x=0;
for(x=0;x<d;x++)
{
printf("Input temperature in Celsius for #%d day",x+1);
scanf("%d",&temp[x]);
}
}
void conv()
{
int x=0;
for(x=0;x<d;x++)
{
temp[x]=1.8*temp[x]+32;
}
}
回答by Some programmer dude
Allocate the "array" dynamically on the heap after you read the size.
读取大小后,在堆上动态分配“数组”。

