我需要创建一个程序,该程序:
- 最初分配一个数组以读取并保持5个温度。
- 提示用户输入温度并在输入温度时输入值-100.0
- 如果用户填写了数组,则程序应 动态分配一个新数组,其大小是原来的两倍。
- 将旧值复制到新数组中。 取消分配旧数组。
- 继续读取新数组。
- 完成后打印出新数组
我对C完全陌生,有点卡住了。我知道如何创建动态数组,但是我不知道如何创建一个新数组,一旦旧数组被填满,该数组就会不断增长。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
int i,k; //loop count
int j = 5; //initial array size
int* temp = malloc(sizeof(int)*j);
int* newtemp;
for (i = 0; i < j; i++){ //loop to read in temperature
printf("enter temperature: ");
scanf("%d",(temp+i));
if (i=j){
j = j*2; //double the size of initial array
int* newtemp = malloc(sizeof(int)*j);
strcpy(*newtemp,temp); // copy string
for (k = 0; k < j; k++){ //loop to read in temperature
printf("enter temperature: ");
scanf("%d",(temp+i+k));
}
}
switch (temp[i]){
case (-100):
temp[i] = '\0';
i = 5; //loop ends
break;
}
}
return 0;
}
错误消息:
tempp.c:18:16: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
strcpy(*newtemp,temp);
^
In file included from tempp.c:3:0:
/usr/include/string.h:121:14: note: expected ‘char * restrict’ but argument is of type ‘int’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^~~~~~
tempp.c:18:25: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type [-Wincompatible-pointer-types]
strcpy(*newtemp,temp);
^~~~
In file included from tempp.c:3:0:
/usr/include/string.h:121:14: note: expected ‘const char * restrict’ but argument is of type ‘int *’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
我知道我的代码很乱,而且我真的不知道在不断增长的同时重新分配新数组的正确方法。 请帮我解决一下这个。谢谢!
答案 0 :(得分:2)
如何使用realloc工具呢?
void printArray(double *array, int size){
for(int i=0; i<size; i++){
printf("%.1lf ", array[i]);
}
putchar('\n');
}
int main(void){
int size = 5;
double *array = malloc(size * sizeof(double));
double temperature;
int i = 0;
while(1){
if(temperature == -100.0)
break;
if(i == size){
size *= 2;
array = realloc(array, size * sizeof(double));
}
scanf("%lf", &temperature);
array[i] = temperature;
printArray(array, size);
i++;
}
free(array);
return 0;
}
答案 1 :(得分:0)
#define INITIALSIZE 5
typedef struct
{
size_t size;
size_t index;
int data[];
}DATA_t;
DATA_t *addData(DATA_t *data, int val)
{
if(!data)
{
data = malloc(INITIALSIZE * sizeof(data -> data[0]) + sizeof(*data));
/* malloc result checks */
data -> size = 0;
data -> index = 0;
}
if((data -> index + 1) == data -> size)
{
size_t newsize = data -> size * 2 ;
DATA_t *newdata = malloc(newsize * sizeof(data -> data[0]) + sizeof(*data));
/* malloc result checks */
memcpy(newdata, data, data -> size * sizeof(data -> data[0]) + sizeof(*data));
newdata -> size = newsize;
free(data);
data = newdata;
}
data -> data[data -> index++] = val;
return data;
}
用法:
DATA_t *mydata = NULL;
while(condition)
{
mydata = addData(mydata, ReadValue());
/* ----- */
}
答案 2 :(得分:0)
您可以尝试声明两个数组,并在它们之间切换,就像这样:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
int currentArraySize = 5;
int* temperatures1 = malloc(sizeof(int)*currentArraySize);
int* temperatures2 = NULL;
int temperaturesSlot = 0;
int temperature = 0;
int index = 0;
while(1){
if (index == currentArraySize){
switch (temperaturesSlot){
case 0:
temperatures2 = malloc(sizeof(int)* 2 *currentArraySize);
memcpy(temperatures2, temperatures1, currentArraySize * sizeof(int));
free(temperatures1);
temperatures1 = NULL;
temperaturesSlot = 1;
break;
case 1:
temperatures1 = malloc(sizeof(int)* 2 *currentArraySize);
memcpy(temperatures1, temperatures2, currentArraySize * sizeof(int));
free(temperatures2);
temperatures2 = NULL;
temperaturesSlot = 0;
break;
}
currentArraySize *= 2;
}
printf("enter temperature: ");
scanf("%d",(&temperature));
if (temperature == -100){
break;
}
else if (temperaturesSlot == 0){
temperatures1[index] = temperature;
}
else{
temperatures2[index] = temperature;
}
++index;
}
for (int i = 0; i < index; ++i){
if (temperaturesSlot == 0){
printf("%d, ", temperatures1[i]);
}
else{
printf("%d, ", temperatures2[i]);
}
}
}
答案 3 :(得分:-2)
如果您是初学者,有一个简单的解决方案。假设输入已经在第一个数组myArray中。
//both arrays can be created dynamically, but are not in this instance for simplicity of the example.
int myArray[5] = { 19,10,11,5,7 }; //initial array of size 5
int doubleMyArray[10] = { 0 }; //array of size double, filled with 0s
int i;
//size of an array: sizeof(myArray)/sizeof(myArray[0])
for (i = 0; i < (sizeof(myArray) / sizeof(myArray[0])); i++) {
doubleMyArray[i] = myArray[i];
}
然后,您可以将新元素(如果有)放入doubleMyArray [i]中,将每个输入(i ++)的i值加1,直到i <[array size]或输入中的用户输入为-100环。
您的阵列并没有持续增长。首先是由5个元素组成的数组,然后是由10个元素组成的新数组。将所有元素移动到第二个数组后,您将从第一个数组中分配内存,并在最后输出新数组,以检查期望的元素是否存在。
此外,没有关于动态分配的信息。如果您创建了一个函数以将前5个输入带入第一个数组(在该函数内本地创建),然后将第二个数组从该函数返回到main()中,且第一个数组的元素已在其中,则第一个数组将自动取消分配自身,仅在main()中留下第二个数组,因为第一个数组仅存在于该函数的范围内。