c中结构的问题

时间:2011-04-28 09:16:05

标签: c

大家好,我在这里遇到了一个问题,结果是,我创建了一个结构,然后创建了一个捕获该结构引用的员工详细信息的函数。现在当我尝试在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();
}

我不知道在主

中传递给函数的参数

5 个答案:

答案 0 :(得分:1)

我已经考虑了其他一些需要考虑的问题

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调用已经被打破了。

答案 1 :(得分:0)

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;

答案 2 :(得分:0)

您可以传递结构

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

void enterDetails(EMPLOYEE *details)
{

}

答案 3 :(得分:0)

你需要传递一个引用,而不是一个值......如果你像上一篇文章一样传递EMPLOYEE值,它将被复制,副本将被修改,而不是原来的

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

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

答案 4 :(得分:0)

第一个问题是你的结构不正确。您不能在名称字段中存储员工的姓名,因为它只有一个字节。你必须使它成为一个数组(在这种情况下更简单)或指向已分配内存的指针。

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

#define MAX_NAME 100

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

其次,你的功能命名令人困惑。 enterDetails应该只填充您传递的结构。第三,您的输入详细信息应接受指向EMPLOYEE结构的指针。如果你想将任何值传递给一个会改变它内容的函数,那么你只能使用指针(或者如果你使用的是C ++但是它基本上是一个指针)。所以enterDetails应该是,

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