将结构传递给Visual Studio中的函数

passing a structure to a function in Visual Studio

本文关键字:Studio 函数 Visual 结构      更新时间:2023-10-16

我在一个项目上工作,我试图将一个结构传递给一个函数,我尝试了各种方法,但我仍然不足。我得到错误信息:

非法使用这种类型的表达式。

非常感谢你的帮助。

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