将结构传递给Visual Studio中的函数
passing a structure to a function in Visual Studio
我在一个项目上工作,我试图将一个结构传递给一个函数,我尝试了各种方法,但我仍然不足。我得到错误信息:
非法使用这种类型的表达式。
非常感谢你的帮助。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct big{
int day;
int year;
char month[10];
} ;
void gen(struct big);
void main()
{
int choice;
printf("ttttt*MENU*nnn");
printf("ttGenerate Buying/Selling Price-------------------PRESS 1nn");
printf("ttDisplay Foreign Exchange Summary----------------PRESS 2nn");
printf("ttBuy Foreign Exchange----------------------------PRESS 3nn");
printf("ttSell Foreign Exchange---------------------------PRESS 4nn");
printf("ttExit--------------------------------------------PRESS 5nnnn");
printf("ttPlease enter your choice");
scanf("%d", &choice);
if (choice == 1)
{
gen(big);
}
system("pause");
}
void gen(big rec)
{
printf("Enter the date in the format: 01-Jan-1993");
scanf("%d %s %d", &rec.day, &rec.month, &rec.year);
}
你试图传递结构体定义本身,创建它的一个实例,然后传递它。
big myBig;
gen(myBig);
a>不创建结构为big
的对象。你可以用big obj;
b> void main
是一种不好的编程方式。至少使用int main(void)
c>传递引用而不是堆栈上的副本
:
void gen(big& obj)
{
printf("Enter the date in the format: 01-Jan-1993");
scanf("%d %s %d", &rec.day, &rec.month, &rec.year);
}
您必须创建一个big
类型的对象。struct big
只是一个房子的蓝图。而下面的bigObject
是房子。保存值等的struct big
类型的实际变量
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct big{
int day;
int year;
char month[10];
};
void gen(struct big);
void main()
{
int choice;
big bigObject;
printf("ttttt*MENU*nnn");
printf("ttGenerate Buying/Selling Price-------------------PRESS 1nn");
printf("ttDisplay Foreign Exchange Summary----------------PRESS 2nn");
printf("ttBuy Foreign Exchange----------------------------PRESS 3nn");
printf("ttSell Foreign Exchange---------------------------PRESS 4nn");
printf("ttExit--------------------------------------------PRESS 5nnnn");
printf("ttPlease enter your choice");
scanf("%d", &choice);
if (choice == 1)
{
gen(bigObject); /*pass bigObject to gen*/
}
system("pause");
return 0;
}
void gen(big& rec)
{
printf("Enter the date in the format: 01-Jan-1993");
scanf("%d %s %d", &rec.day, &rec.month, &rec.year);
}
相关文章:
- Visual Studio中的函数声明和函数定义问题
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- Visual Studio 中的 sqrtf 函数替代
- 我可以在Visual Studio 2019上的项目下使用多个主函数吗?
- Visual Studio 无法解决类和命名空间中重载的明确函数
- 如何使用Visual Studio 2008将字符串映射到函数?
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- 请参阅在 Visual Studio 2019 中捕获 std::exception 时对函数模板实例化消息的引用
- 无法在clang Linux中分配非静态成员函数,但我可以在Visual Studio Windows中分配
- Visual Studio在创建类时不再生成构造函数/解构器
- 使用函数从 Visual Studio 2017 中的 main 创建和初始化数组
- C++ Visual Studio 重载函数错误:没有重载函数的实例与指定的类型匹配
- 为什么Visual Studio允许我在模板函数中使用私有成员(C++)?
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- CGAL 绘制函数在 Visual Studio 中给出'CGAL_USE_BASIC_VIEWER is not defined'错误
- 如何在 Linux 中运行 Visual Studio 2015 托管 c++ 测试函数?
- Wow64DisableWow64FsRedirection() 函数在窗口 10 的 Visual Studio 中编
- Visual Studio函数参数缩进
- visual studio-C++函数调用顺序(boost)-混乱
- Visual Studio函数调试