c语言问题 无法运行

c语言问题 无法运行

问题描述:

#include<stdio.h>
#include<stdlib.h>


int main(void){
    int size = 2;
    float* ptr;
    float  t;
    int count = 0;

    ptr = calloc(size, sizeof(float)); // dynamic memory allocation for an array to intial size of 5
    while(1){  
        if(t == -100.0)             // While loop repeats till a value of -100.0 is entered
            break;
        if(count == size){
            size *= 2;
            ptr = realloc(ptr, size * sizeof(float));   // reallocation of memory to array by double the size
        }
        scanf("%f", &t);
        ptr[count] = t;
        count++;            // counting array elements
    }
  printf("You have entered the following temperatures : ");
    for(int i=0; i<count-1; i++){
        printf("%.1lf ", ptr[i]);               // printing temperature values
    }
    printf("\nTemperatures in reverse order : ");
    for(int i = count-2; i>=0; i--){
        printf("%.1f\t", ptr[i]);           // printing temperature values in reverse
    }
    free(ptr);
    return 0;
}

一个是t没有初始化,二是ptr需要强制类型转换一下。代码修改如下,如有帮助,请采纳一下,谢谢。

#include<stdio.h>
#include<stdlib.h>
int main(void){
	int size = 2;
	float* ptr;
	float  t = 0.0;
	int count = 0;
	ptr = (float*)calloc(size, sizeof(float)); // dynamic memory allocation for an array to intial size of 5
	while(1){  
		if(t == -100.0)             // While loop repeats till a value of -100.0 is entered
			break;
		if(count == size){
			size *= 2;
			ptr = (float*)realloc(ptr, size * sizeof(float));   // reallocation of memory to array by double the size
		}
		scanf("%f", &t);
		ptr[count] = t;
		count++;            // counting array elements
	}
	printf("You have entered the following temperatures : ");
	for(int i=0; i<count-1; i++){
		printf("%.1lf ", ptr[i]);               // printing temperature values
	}
	printf("\nTemperatures in reverse order : ");
	for(int i = count-2; i>=0; i--){
		printf("%.1f\t", ptr[i]);           // printing temperature values in reverse
	}
	free(ptr);
	return 0;
}

t要初始化一下,t=0;

我运行你的代码没问题:

1 2 3 4 5 6 7 8 9 -100
You have entered the following temperatures : 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 
Temperatures in reverse order : 9.0     8.0     7.0     6.0     5.0     4.0     3.0     2.0     1.0

首先说问题:

你用的calloc函数返回类型是void*(realloc函数类似)

原形是:void* calloc(unsigned int num,unsigned int size);

功能是:在内存的动态存储区中分配num个长度为size的连续空间;

那么实际上相当于你在创建了一个float的指针p,然后想要把一个通过函数创建的void的指针赋给p。

要怎么办呢?只要加入强制类型转换就可以了。

```cpp
#include<stdio.h>
#include<stdlib.h>

int main(void){
    int size = 2;
    float *ptr;
    float  t;
    int count = 0;
    ptr = (float*)calloc(size,sizeof(float)); // dynamic memory allocation for an array to intial size of 5
    while(1){
        if(t == -100.0)             // While loop repeats till a value of -100.0 is entered
            break;
        if(count == size){
            size *= 2;
            ptr = (float*)realloc(ptr, size * sizeof(float));   // reallocation of memory to array by double the size
        }
        scanf("%f", &t);
        ptr[count] = t;
        count++;            // counting array elements
    }
  printf("You have entered the following temperatures : ");
    for(int i=0; i<count-1; i++){
        printf("%.1lf ", ptr[i]);               // printing temperature values
    }
    printf("\nTemperatures in reverse order : ");
    for(int i = count-2; i>=0; i--){
        printf("%.1f\t", ptr[i]);           // printing temperature values in reverse
    }
    free(ptr);
    return 0;
}

```

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632