我需要创建一个程序来:
我对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)我知道我的代码很混乱,我真的不知道在新数组不断增长的时候重新分配新数组的正确方法。请帮我处理这个。谢谢!
发布于 2019-05-22 12:29:20
不如用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;
}发布于 2019-05-22 11:11:23
#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());
/* ----- */
}发布于 2019-05-22 12:16:12
您可以尝试声明两个数组,并在它们之间切换,如下所示:
#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]);
}
}
}https://stackoverflow.com/questions/56254128
复制相似问题