【发布时间】:2018-03-13 20:20:36
【问题描述】:
再次需要帮助。用户在 docreate() 函数中输入了一些值,我需要将这些值返回到主函数中来打印它们。我已经尝试但无法实现目标。当用户在主代码中输入 2 时,我现在只是无人机(名称)的一个特征,用于打印。代码如下:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct drone_t{
char name[30];
float top_s;
float acc;
};
struct do_create(int dronesCreated);
#define MAXDRONES 3
int main()
{
struct drone_t drone;
int dronesCreated = 0;
int i;
char namee;
while(1)
{
printf("1. Create Drone\n2. Calculate Time\n3. Exit\n");
scanf("%d", &i);
if (i == 1)
{
if(dronesCreated<=MAXDRONES-1)
{
dronesCreated++;
do_create(dronesCreated);
}
else
{
printf("error: cannot create more drones\n");
}
}
else if (i == 2)
{
printf("%s", drone[dronesCreated].name);
}
else if (i == 3)
{
exit(EXIT_SUCCESS);
}
else
{
printf("error: select an option between 1 and 3\n");
}
}
}
void do_create(int dronesCreated)
{
struct drone_t drone[dronesCreated];
printf("What is the name of the drone?\n");
scanf("%s", drone[dronesCreated].name);
printf("What is the top speed of the drone? (kmph)\n");
scanf("%f", &drone[dronesCreated].top_s);
printf("What is the acceleration of the drone? (mpsps)\n");
scanf("%f", &drone[dronesCreated].acc);
return drone.name;
}
【问题讨论】:
-
struct do_create(int dronesCreated);- 不是任何有效的声明。 -
它是 void do_create (intdronesCreated);之前。我将其更改为移动值,但无论如何它都不起作用。
-
数组必须在
main中定义。如果函数的返回类型为void,则不能返回值。 -
我在这方面遇到了困难,你能提供一个关于如何做到这一点的行或示例吗?
-
示例code
标签: c arrays function pointers structure