繁体   English   中英

在gcc ver 4.6.3中按值将Struct作为参数传递给函数时出错

[英]Error in passing Struct as argument to a function by value in gcc ver 4.6.3

我是C编程的新手,我正在学习按值将结构作为参数传递给函数(作为课程的一部分)。 我在Ubuntu Linux 12.04LTS上使用gcc ver 4.6.3,以下是对我来说在逻辑和语法上均正确的源代码,但是在编译时出现错误:

#include<stdio.h>

struct sal {
    char name[30];
    int no_of_days_worked;
    int daily_wage;
};
typedef struct sal Sal;

void main()
{
    Sal salary;
    int amount_payable;
    salary=get_data(salary);           //Passing struct as function arguments
    printf("\nThe name of the Employee is %s",salary.name);
    printf("\nNumber of days worked is %d",salary.no_of_days_worked);
    printf("\nThe daily wage of the employees is %d",salary.daily_wage);
    amount_payable=wages(salary);
    printf("\nThe amount payable to %s is %d",salary.name,amount_payable);
}

Sal get_data(Sal income)
{
    printf("\nEnter the name of the Employee: \n");
    scanf("%s",&income.name);
    printf("\nEnter the number of days worked:\n");
    scanf("%d",&income.no_of_days_worked);
    printf("\nEnter the employee daily wages:\n");
    scanf("%d",&income.daily_wage);
    return(income);                                //Return back a struct data type
}

int wages(Sal x)
{
    int total_salary;
    total_salary=x.no_of_days_worked*x.daily_wage;
    return(total_salary);
} 

在编译代码时,出现以下错误:

struct_to_function.c: In function ‘main’:
struct_to_function.c:15:7: error: incompatible types when assigning to type ‘Sal’ from type ‘int’
struct_to_function.c: At top level:
struct_to_function.c:22:5: error: conflicting types for ‘get_data’
struct_to_function.c:15:8: note: previous implicit declaration of ‘get_data’ was here
struct_to_function.c: In function ‘get_data’:
struct_to_function.c:25:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Wformat]

我认为这与gcc编译器的实现或执行计划有关,无论该编译器使用的是堆栈还是寄存器。 同样,这些只是我的业余假设。

当C编译器遇到从main调用get_data时,它不知道返回类型是什么(因为它既没有看到函数声明也没有看到函数定义),因此它假定为int 这是第一个警告,因为salary与任务中的int不兼容。 编译器继续前进,现在认为get_data返回int ,然后在遇到get_data的实际定义时抱怨。

您应该在main之前添加一个函数原型,或者确保在调用之前始终定义函数(通过在源代码中重新排列它们的顺序)。

最后的警告是因为带有%s说明符的scanf期望使用char* ,但是您给它指定了char (*)[30] 传递数组时,请省略&

只需在main之前添加以下内容:

Sal get_data(Sal income);
int wages(Sal x);

您必须将指针传递给该结构。 当您尝试传递结构本身时,C编译器会尝试对其进行复制,但是它不知道该怎么做(您将需要使用C ++来定义它),因此是错误的。

在使用函数之前没有声明它们,因此编译器为您创建了它自己的默认定义。 您必须先声明一个函数,然后才能引用它。

同样,不好的做法是按值传递结构,然后对其进行修改然后传递回。 最好只是将指针传递到要修改的对象。

所以:

int wages( Sal x );
void get_data( Sal* income );
void main()
{
    Sal salary;
    int amount_payable;
    get_data( &salary );           //Passing struct as function arguments
    // print stuff
    amount_payable = wages( salary );
    // print stuff
}
void get_data( Sal* income )
{
    printf( "\nEnter the name of the Employee: \n" );
    scanf( "%s", income->name );
    printf( "\nEnter the number of days worked:\n" );
    scanf( "%d", &(income->no_of_days_worked) );
    printf( "\nEnter the employee daily wages:\n" );
    scanf( "%d", &(income->daily_wage) );
}
int wages( Sal x )
{
    int total_salary;
    total_salary = x.no_of_days_worked * x.daily_wage;
    return total_salary;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM