0

我正在做一个项目,我试图将一个结构传递给一个函数,我尝试了各种方法,但我仍然做不到。我收到错误消息:

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

非常感谢您的帮助。

    #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("\t\t\t\t\t*MENU*\n\n\n");
printf("\t\tGenerate Buying/Selling Price-------------------PRESS 1\n\n");
printf("\t\tDisplay Foreign Exchange Summary----------------PRESS 2\n\n");
printf("\t\tBuy Foreign Exchange----------------------------PRESS 3\n\n");
printf("\t\tSell Foreign Exchange---------------------------PRESS 4\n\n");
printf("\t\tExit--------------------------------------------PRESS 5\n\n\n\n");
printf("\t\tPlease 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);
}
4

2 回答 2

2

您正在尝试传递结构定义本身,创建它的一个实例然后传递它。

big myBig;
gen(myBig);
于 2013-03-04T02:08:43.927 回答
0

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

您必须创建一个类型为 的对象bigstruct big只是房子的蓝图。而bigObject下面房子。struct bigHOLDS 值等的实际类型变量。

#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("\t\t\t\t\t*MENU*\n\n\n");
    printf("\t\tGenerate Buying/Selling Price-------------------PRESS 1\n\n");
    printf("\t\tDisplay Foreign Exchange Summary----------------PRESS 2\n\n");
    printf("\t\tBuy Foreign Exchange----------------------------PRESS 3\n\n");
    printf("\t\tSell Foreign Exchange---------------------------PRESS 4\n\n");
    printf("\t\tExit--------------------------------------------PRESS 5\n\n\n\n");
    printf("\t\tPlease 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);
}
于 2013-03-04T02:20:09.897 回答