c中的结构问题

c中的结构问题,c,C,大家好,我对结构有一个问题,问题是,我创建了一个结构,然后创建了一个函数来捕获从该结构引用的员工详细信息。现在,当我尝试调用main中的函数时,问题来了。请给我一些关于如何调用函数的提示。代码如下: typedef struct employeeType { char name; int employeeNumber; float salary; float taxPercentage; }EMPLOYEE; void enterDetails(EMPLOYEE

大家好,我对结构有一个问题,问题是,我创建了一个结构,然后创建了一个函数来捕获从该结构引用的员工详细信息。现在,当我尝试调用main中的函数时,问题来了。请给我一些关于如何调用函数的提示。代码如下:

typedef struct employeeType
{
    char name;
    int employeeNumber;
    float salary;
    float taxPercentage;
}EMPLOYEE;

void enterDetails(EMPLOYEE details)
{  
    FILE *file;
    file = fopen("employees.txt","w");
    if(file == NULL)
    {
        printf("File error!!!");
        exit(0);
    }
    else
    {
        fprintf(file,"%s",details);
    }
    fclose(file);

}

void main()
{ 
  enterDetails();
}
我不知道在main中传递给函数的参数是什么

void main()
{ 
  EMPLOYEE details;
   // get the value of element of struct from scanf or from other way
   printf("Enter Name : ");
   scanf("%s", details.name);  // same for others, change the format specifier according to their data type
  enterDetails(details);
}
结构应该是这样的

typedef struct employeeType
{
    char name[];  // should be an array or pointer, to store name
    int employeeNumber;
    float salary;
    float taxPercentage;
}EMPLOYEE;
结构应该是这样的

typedef struct employeeType
{
    char name[];  // should be an array or pointer, to store name
    int employeeNumber;
    float salary;
    float taxPercentage;
}EMPLOYEE;

您可以传递结构的指针

void main()
{
    EMPLOYEE employee;
    .....
    enterDetails(&employee);
}

void enterDetails(EMPLOYEE *details)
{

}

您可以传递结构的指针

void main()
{
    EMPLOYEE employee;
    .....
    enterDetails(&employee);
}

void enterDetails(EMPLOYEE *details)
{

}

我已经为您的代码添加了一些其他需要考虑的问题

typedef struct employeeType
{
     /* THIS IS ONLY ONE CHARACTER... SEEMS WRONG */
     /* should be 'char name[someMaxSize]', or 'char *name' */
    char name;  
    int employeeNumber;
    float salary;
    float taxPercentage;
}EMPLOYEE;

/* As pointed out by 'Cody Gray', this function is called 'enterDetails'
 * does it really need to have a parameter at all, or should it be responsible
 * for taking the details from the user?  Is it an appropriately 
 * named method for the task it's actually performing 
 * (would saveDetails be better for example)?
 */
void enterDetails(EMPLOYEE details)
{  
    FILE *file;
    file = fopen("employees.txt","w");
    if(file == NULL)
    {
        printf("File error!!!");
        exit(0);
    }
    else
    {
        /* THIS IS PASSING A STRUCTURE AS A STRING */
              /* You probably want to write out the individual fields instead */
              /* fprintf(file, "%s,%d", details.name, details.employeeNumber); etc */
        fprintf(file,"%s",details);  
    }
    fclose(file);

}

void main()
{ 
  EMPLOYEE details;   
  /* populate details somehow then pass it in to the function*/ 
  enterDetails(details);
}

您可能还想考虑将细节传递到函数中作为指针,虽然这会改变函数签名,但这意味着您不会将大量信息推送到堆栈上。

如果使用指针版本,则:

void enterDetails(EMPLOYEE details) 
将成为

void enterDetails(EMPLOYEE *details) 
主要问题是:

void main()
{ 
  EMPLOYEE details;   
  /* populate details somehow then pass it in to the function as pointer */ 
  enterDetails(&details);
}

您还需要更改在函数中使用细节的方式,但正如我已经说过的,我相信您的fprintf调用已经中断。

我已经为您的代码注释了一些其他需要考虑的问题

typedef struct employeeType
{
     /* THIS IS ONLY ONE CHARACTER... SEEMS WRONG */
     /* should be 'char name[someMaxSize]', or 'char *name' */
    char name;  
    int employeeNumber;
    float salary;
    float taxPercentage;
}EMPLOYEE;

/* As pointed out by 'Cody Gray', this function is called 'enterDetails'
 * does it really need to have a parameter at all, or should it be responsible
 * for taking the details from the user?  Is it an appropriately 
 * named method for the task it's actually performing 
 * (would saveDetails be better for example)?
 */
void enterDetails(EMPLOYEE details)
{  
    FILE *file;
    file = fopen("employees.txt","w");
    if(file == NULL)
    {
        printf("File error!!!");
        exit(0);
    }
    else
    {
        /* THIS IS PASSING A STRUCTURE AS A STRING */
              /* You probably want to write out the individual fields instead */
              /* fprintf(file, "%s,%d", details.name, details.employeeNumber); etc */
        fprintf(file,"%s",details);  
    }
    fclose(file);

}

void main()
{ 
  EMPLOYEE details;   
  /* populate details somehow then pass it in to the function*/ 
  enterDetails(details);
}

您可能还想考虑将细节传递到函数中作为指针,虽然这会改变函数签名,但这意味着您不会将大量信息推送到堆栈上。

如果使用指针版本,则:

void enterDetails(EMPLOYEE details) 
将成为

void enterDetails(EMPLOYEE *details) 
主要问题是:

void main()
{ 
  EMPLOYEE details;   
  /* populate details somehow then pass it in to the function as pointer */ 
  enterDetails(&details);
}

您还需要更改在函数中使用细节的方式,但正如我已经说过的,我相信您的fprintf调用已经中断。

您需要传递一个引用,而不是一个值。。。如果您像在上一篇文章中一样传递员工值,则将复制该值,并修改副本,而不是原始值

 void enterDetails(EMPLOYEE* emp) {
    // do stuffs
 }

 void main() {
   EMPLOYEE emp;
   enterDetails(&emp);
 }

您需要传递引用,而不是值。。。如果您像在上一篇文章中一样传递员工值,则将复制该值,并修改副本,而不是原始值

 void enterDetails(EMPLOYEE* emp) {
    // do stuffs
 }

 void main() {
   EMPLOYEE emp;
   enterDetails(&emp);
 }

第一个问题是你的结构不正确。您不能将员工的姓名存储在“姓名”字段中,因为它只有一个字节。您必须将其设置为数组(在本例中更简单)或指向已分配内存的指针

如果要使其成为数组,则应定义数组的最大大小。在我们的示例中,我们只需要将其设置为100字节,就足以存储任何名称

#define MAX_NAME 100

typedef struct employeeType
{
    char name[MAX_NAME];
    int employeeNumber;
    float salary;
    float taxPercentage;
}EMPLOYEE;
其次,你会发现函数命名很混乱。enterDetails应该只填充您传递的结构。第三,输入详细信息应该接受指向员工结构的指针。如果你想把任何值传递给一个函数来改变它的内容,那么你只能使用指针(或者如果你使用C++,但基本上是指针)。所以细节应该是,

void enterDetails(EMPLOYEE *details)
{
    printf("\nEnter the employee's name ");
    scanf("%s", details->name); // this isn't secure since it doesn't perform bound checking.

    printf("\nEnter employee number ");
    scanf("%d", &details->employeeNumber);

    printf("\nEnter employee salary ");
    scanf("%f", &details->salary);

    printf("\nEnter tax percentage ");
    scanf("%f", &details->taxPercentage);

}
最后,如果您想将结构的内容存储到一个文件中,让人们阅读,那么您应该格式化结构的内容并将其转储到一个文件中

int writeToFile(EMPLOYEE *details) /* accepting the structure will work as well but it's faster and efficient to pass the structure's pointer */    
{
    FILE *file;

    file = fopen("employees.txt","w");
    if(file == NULL) {
       printf("File error!!!");
       return 0;
    }

    fprintf(file, "\nEmployee Name: %s", details->name);
    fprintf(file, "\nEmployee Number: %d", details->employeeNumber);
    fprintf(file, "\nSalary: %f", details->salary);
    fprintf(file, "\nTax Percentage: %f", details->taxPercentage);

    fclose(file)
    return 1;
}
和主要

int main(void)
{
    EMPLOYEE details;

    enterDetails(&details); // passing the pointer here is a must
    if (!writeToFile(&details)) { // passing the pointer since it's faster
       printf("\nError writing to file");
       return 1;
    } else {
       printf("\nSuccess!");
       return 0;
    }
}
在您的情况下,不需要将任何参数传递给main。但是,如果您想知道如何传递参数,那么下面是一个快速示例

int main(int argc, char **argv)
{
    int i;

    for (i = 0; i < argc; i++)
        printf("\n%s", argv[i]);

    return 0;
}
int main(int argc,char**argv)
{
int i;
对于(i=0;i
第一个问题是您的结构不正确。您不能将员工的姓名存储在“姓名”字段中,因为它只有一个字节。您必须将其设置为数组(在本例中更简单)或指向已分配内存的指针

如果要使其成为数组,则应定义数组的最大大小。在我们的示例中,我们只需要将其设置为100字节,就足以存储任何名称

#define MAX_NAME 100

typedef struct employeeType
{
    char name[MAX_NAME];
    int employeeNumber;
    float salary;
    float taxPercentage;
}EMPLOYEE;
其次,你会发现函数命名很混乱。enterDetails应该只填充您传递的结构。第三,输入详细信息应该接受指向员工结构的指针。如果你想把任何值传递给一个函数来改变它的内容,那么你只能使用指针(或者如果你使用C++,但基本上是指针)。所以细节应该是,

void enterDetails(EMPLOYEE *details)
{
    printf("\nEnter the employee's name ");
    scanf("%s", details->name); // this isn't secure since it doesn't perform bound checking.

    printf("\nEnter employee number ");
    scanf("%d", &details->employeeNumber);

    printf("\nEnter employee salary ");
    scanf("%f", &details->salary);

    printf("\nEnter tax percentage ");
    scanf("%f", &details->taxPercentage);

}
最后,如果您想将结构的内容存储到一个文件中,让人们阅读,那么您应该格式化结构的内容并将其转储到一个文件中

int writeToFile(EMPLOYEE *details) /* accepting the structure will work as well but it's faster and efficient to pass the structure's pointer */    
{
    FILE *file;

    file = fopen("employees.txt","w");
    if(file == NULL) {
       printf("File error!!!");
       return 0;
    }

    fprintf(file, "\nEmployee Name: %s", details->name);
    fprintf(file, "\nEmployee Number: %d", details->employeeNumber);
    fprintf(file, "\nSalary: %f", details->salary);
    fprintf(file, "\nTax Percentage: %f", details->taxPercentage);

    fclose(file)
    return 1;
}
和主要

int main(void)
{
    EMPLOYEE details;

    enterDetails(&details); // passing the pointer here is a must
    if (!writeToFile(&details)) { // passing the pointer since it's faster
       printf("\nError writing to file");
       return 1;
    } else {
       printf("\nSuccess!");
       return 0;
    }
}
在您的情况下,不需要将任何参数传递给main。但是,如果您想知道如何传递参数,那么下面是一个快速示例

int main(int argc, char **argv)
{
    int i;

    for (i = 0; i < argc; i++)
        printf("\n%s", argv[i]);

    return 0;
}
int main(int argc,char**argv)
{
int i;
对于(i=0;i
你的意思可能是
intmain(){}
@Cody:
intmain(){}
不会有太大作用;-)人力资源管理,至少它是编译的。我认为这显然不是重点。你可能指的是int main(int argc,char*argv[]),因为你们都不知道这是托管应用还是无主机应用,smarta**对main()的评论与在托管程序中使用void main()一样无知。阅读。你的意思可能是
intmain(){}
@Cody:
intmain(){}
不会有太大作用;-)人力资源管理,至少它是编译的。我认为这显然不是重点。你可能指的是int main(int argc,char*argv[]),因为你们都不知道这是托管应用还是无主机应用,smarta**对main()的评论与在托管程序中使用void main()一样无知。读取。它们正在写入文件,而不是填充结构,因此函数不会以任何方式修改内容。@forsvarir:那么如果写入文件,为什么
enterDetails
函数会接受任何参数?看起来它应该创建一个结构实例,填写它,保存到一个文件中,然后返回。@Cody Gray:一个合理的问题。它可能名称不正确,意味着将详细信息输入到文件中,也可能是因为您是正确的,enterDetails函数应该询问信息以便能够将其写入文件。。。我