kkdjm 2021-05-23 21:08 采纳率: 100%
浏览 53
已采纳

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;
}

  • 写回答

5条回答 默认 最新

  • qfl_sdu 2021-05-23 21:24
    关注

    一个是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;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?