c - C程序返回矩形的周长和面积
问题描述
我想编写 ac 程序来打印矩形的面积和周长,但出现分段错误错误。
#include <stdio.h>
int rectanglePerimeter(int h, int w)
{
int p =(2*h)+(2*w);
return p;
}
int rectangleArea(int h, int w)
{
int a = h * w;
return a;
}
int main()
{
int h, w, perimeter, area;
printf("Tell me the height and width of the rectangle\n");
scanf("%i", h);
scanf("%i", w);
perimeter = rectanglePerimeter(h, w);
area = rectangleArea(h, w);
printf("Perimeter of the rectangle = %i inches", perimeter);
printf("Area of the rectangle = %i square inches", area);
return 0;
}
有人可以向我解释我做错了什么吗?
解决方案
在使用 C 编程语言时,某些函数需要使用内存地址,scanf
函数就是这种情况。Scanf 函数接受参数,第一个是您期望的数据类型,int、float、char 等;第二个参数是你将保存这个输入的地方,这个地方不是你要保存的变量的引用,而是变量的内存地址。这种方式scanf
应将读取的值保存在您存储的变量中。
scanf("%i", &h);
scanf("%i", &w);
推荐阅读
- c# - 如何将参数传递给通用动作过滤器
- google-cloud-platform - 关于按硬件资源收取的 Cloud Run 费用:我是否需要为未充分使用的内存付费?
- python - 卡在虚拟环境中启动子shell
- python - 在 Python 中绘制多变量线性回归
- reactjs - 将 setState 调用到其原始数组后,如何使映射的输入文件重新呈现?
- node.js - 如何将多个嵌入集合中的数据获取到 ejs 中?
- excel - 有没有办法让 Excel 实例保持打开状态,即使所有工作簿都已关闭?
- unity3d - 无法在 ubuntu 上访问统一包管理器
- ruby - DocuSign / Ruby - 尝试获取令牌时不支持_grant_type
- flutter - 错误“pumpAndSettle 超时”可能是由于riverpod