为什么我们不能将结构作为返回值返回?我也想打印它?我想打印返回结构的变量。
#include <stdio.h>
struct distance
{
int feet;
float inch;
};
distance add(struct distance d1,struct distance d2);
distance d3;
int main()
{
struct distance dist1, dist2,dist3;
printf("first distance\n");
scanf("%d",&dist1.feet);
scanf("%f",&dist1.inch);
printf("second distance\n");
scanf("%d",&dist2.feet);
scanf("%f",&dist2.inch);
dist3 = add(dist1,dist2);
printf("sum of distances= %d\'-%.1f\"",dist3.feet,dist3.inch);
return dist3;
}
distance add(struct distance d1,struct distance d2){
//Adding distances d1 and d2 and storing it in d3
d3.feet = d2.feet+d1.feet;
d3.inch = d2.inch+d1.inch;
if(d3.inch>12){
d3.inch-=12;
++d3.feet;
}
}
显示以下错误消息 prog.c:7:1:错误:未知类型名称'距离' 距离加(结构距离d1,结构距离d2);
答案 0 :(得分:3)
是的,当然你可以返回一个结构,但你必须使用return
语句实际返回它:
struct distance add(struct distance d1, struct distance d2)
{
d1.feet += d2.feet;
d1.inches += d2.inches;
while(d1.inches > 12)
{
++d1.feet;
d1.inches -= 12;
}
return d1; /* Return the result. */
}
创建d3
(当然不是全局!)也没有意义,只需使用其中一个参数作为新值,然后使用return
。
使用循环调整溢出更好,所以我也这样做了。
最后,您不要typedef
struct distance
,因此您只能说distance
作为返回类型,它必须是struct distance
喜欢这些论点。
答案 1 :(得分:2)
你需要退货
distance add(struct distance d1,struct distance d2){
struct distance d3;
//Adding distances d1 and d2 and storing it in d3
d3.feet = d2.feet+d1.feet;
d3.inch = d2.inch+d1.inch;
if(d3.inch>12){
d3.inch-=12;
++d3.feet;
}
return d3;
}
答案 2 :(得分:0)
要解决此错误消息:
prog.c:7:1: error: unknown type name ‘distance’ distance add(struct distance d1,struct distance d2)
您需要意识到struct distance
和distance
类型之间存在差异。它们不一样。
鉴于:
struct distance
{
int feet;
float inch;
};
这是不正确的,因为没有 distance
类型:
distance add(struct distance d1,struct distance d2);
该代码假定distance
是一种类型。不是。正确的类型是struct distance
:
struct distance add(struct distance d1,struct distance d2);
您 也可以使用typedef
typedef struct distance
{
int feet;
float inch;
} distance;
就个人而言,我不喜欢以这种方式隐藏类型 - 我更愿意看到类似struct distance
的内容并知道它是struct
。
答案 3 :(得分:0)
#include<stdio.h>
struct distance
{
int feet;
float inch;
};
struct distance d1,d2,d3;
struct distance add(struct distance d1, struct distance d2);
int main()
{
struct distance dist1, dist2,dist3;
printf("first distance\n");
scanf("%d",&dist1.feet);
scanf("%f",&dist1.inch);
printf("second distance\n");
scanf("%d",&dist2.feet);
scanf("%f",&dist2.inch);
dist3 = add(dist1,dist2);
printf("sum of distances= %d\'-%.1f\"",dist3.feet,dist3.inch);
}
struct distance add(struct distance d1,struct distance d2){
//Adding distances d1 and d2 and storing it in d3
d3.feet = d2.feet+d1.feet;
d3.inch = d2.inch+d1.inch;
while(d3.inch>12){
d3.inch-=12;
++d3.feet;
return d3;
}
}
是的,它得到了!这是以下输入的最终输出
输入:
11个
2.3
11
10
这是输出:
距离之和= 23'-0.3“
答案 4 :(得分:-1)
if application.respondsToSelector("registerUserNotificationSettings:") {
if #available(iOS 8.0, *) {
let types:UIUserNotificationType = ([.Alert, .Sound, .Badge])
let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
}
}
else {
// Register for Push Notifications before iOS 8
application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
}
然后摆脱&#39;结构&#39;其他地方的声明......
注意:非常感谢@Lundin进行更正:)