【问题标题】:The following C code shows that : format '%c' expects argument of type 'int', but argument 3 has type char*以下 C 代码显示:格式“%c”需要“int”类型的参数,但参数 3 的类型为 char*
【发布时间】:2020-10-21 08:10:42
【问题描述】:

以下代码显示格式“%c”需要“int”类型的参数,但参数 3 的类型为“int”。我没有看到变量类型有任何错误。此问题出现在 printf 的第 155 行的 void list() 部分。我不知道为什么它期望它为 int 类型。不应该是char吗?这似乎与其他警告不同。请问有人可以帮忙吗?

这是结构:

struct employee
{
    char name[50];
    char sex[7];
    char adrs[50];
    char dsgn[25];
    int age,empID;
    float slry;
};

问题:

while(fread(&e,recsize,1,fptr)==1)///read the file and fetch the record one record per fetch
{
    printf("\n\n%s \t\t%c \t%s \t%s \t%d \t%.2f \t%d",e.name, e.sex,
           e.adrs, e.dsgn, e.age, e.slry, e.empID);
}
getch();

完整代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdbool.h>
#include <windows.h>
#include "struct.h"

void insert();
void list();
void edit();
void del();
void exit();
int tolower();

FILE * fptr, *ftemp;
struct employee e;
long int recsize;
char empname[50];

int main()
{
    int choice;
    fptr = fopen("ems.txt", "r+");


    if (fptr == NULL)
    {
        printf("Can't find file! Attempting to create file... \n");

        fptr = fopen("ems.txt","w+");
        if(fptr == NULL)
        {
            printf("Can't create file. Exiting...");
         exit(1);
        }
    }

    //Explain the reason for this?
    //recsize = (long int) sizeof(e);//


    while(1)
    {
        printf("*******************************\n");
        printf("\nEmployee management system");
        printf("\n1. Insert employee information");
        printf("\n2. List all employee information");
        printf("\n3. Edit employee information");
        printf("\n4. Delete employee information");
        printf("\n5. Exit");
        printf("\n\n*****************************\n");
        printf("\n\n Enter your choice: ");
        scanf("%d", &choice);
        int ch;  while( ( ch = getchar() ) != EOF && ch != '\n' ){;}

        switch(choice)
        {
            case 1:
                puts("Insert was chosen");
                insert();

                break;
            case 2:
                puts("List was chosen");
                list();
                break;
            case 3:
                puts("Edit was chosen");
                edit();
                break;
            case 4:
                puts("Delete was chosen");
                del();
                break;
            case 5:
                puts("Exit was chosen");
                exit(1);
                break;
            default:
                puts("Choice is incorrect!!");
                break;
        }
    }

    return 0;
}


void insert()
{
    char next;

    do
    {
        printf("********************************************************** \n");
        printf("\nEnter the name of the employee: ");
        fgets(e.name, sizeof(e.name), stdin);
        printf("\nEnter the sex of the employee (M/m or F/f): ");
        scanf("%c",e.sex);

        switch(*e.sex)
        {
            case 'M':
            case 'm':
                printf("\nMale.\n");
                break;
            case 'F':
            case 'f':
                printf("\nFemale.\n  ");
                break;
            default:
                printf("Unspecified Sex.");
        }

        printf("\nEnter the address of the employee: ");

        fseek(stdin, 0, SEEK_END); // ADD THIS TO AVOID SKIP

        fgets(e.adrs, sizeof(e.adrs), stdin); // this
        printf("\nEnter designation of the employee: ");
        fgets(e.dsgn, sizeof(e.dsgn), stdin); // this

        printf("\nEnter age of the employee: ");
        scanf("%d", &e.age);
        printf("\nEnter basic salary of the employee: ");
        scanf("%f", &e.slry);
        printf("\nEnter the employee's ID: ");
        scanf("%d", &e.empID);
        fputs(e.name, fptr);
        fputs(e.sex, fptr);
        fputs(e.adrs, fptr);
        fputs(e.dsgn, fptr);
        fprintf(fptr, "%d \n%f \n%d \n", e.age, e.slry, e.empID);
       // fwrite(&e,recsize,1,fptr);
        int ch;  while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
        //fflush(stdin);//
        printf("\nDo you want to input more? (y/n): ");
        next = getche();
        printf("\n");
    }
    while( tolower(next) != 'n' );

    fclose(fptr);
}

void list ()
{
     printf("-------------------------------");
     printf("\nEmployee Details: \n---------------------------------\n");
     rewind(fptr);///moves file to start of the file
     while(fread(&e,recsize,1,fptr)==1)///read the file and fetch the record one record per fetch
     {
         printf("\n\n%s \t\t%c \t%s \t%s \t%d \t%.2f \t%d",e.name, e.sex, e.adrs, e.dsgn, e.age, e.slry, e.empID);
     }
     getch();

     /*printf("Name        : %s\n",e.name);
     printf("Address     : %s\n",e.adrs);
     printf("Sex         : %c\n",e.sex);
     printf("Designation : %s\n",e.dsgn);
     printf("Age         : %d\n",e.age);
     printf("Salary      : %.2f\n",e.slry);
     printf("Employee-ID : %d\n",e.empID);*/
}

void edit ()
{
    char next;
    do
    {
        printf("Enter the employee's name to be edited: ");
        scanf("%49[^\n]", empname);
        rewind(fptr);
        while(fread(&e, recsize, 1, fptr)==1)///fetch all records from file
        {
            if(strcmp(e.name,empname) == 0) ///if entered name matches with that in file
                printf("\nEnter new name, sex, address, designation, age, salary and employee ID: ");
                scanf("%s%c%s%s%d%f%d", e.name, e.sex, e.adrs, e.dsgn, &e.age, &e.slry, &e.empID);
                fseek(fptr, -recsize, SEEK_CUR);/// move cursor 1 step back from current position
                fwrite(&e, recsize,1,fptr); ///override the record
                break;
        }

        printf("\nEdit another record(y/n)");
        next = getche();
        int ch;  while( ( ch = getchar() ) != EOF && ch != '\n' ){;}

    }
    while(next != 'n');

    return ;
}

void del()
{
    char next;
    do
    {
        printf("\nEnter name of employee to delete: ");
        scanf("%s",empname);
        ftemp = fopen("Temp.dat","wb"); ///create a intermediate file for temporary storage
        rewind(fptr); ///move record to starting of file
        while(fread(&e,recsize,1,fptr) == 1)  ///read all records from file
        {
            if(strcmp(e.name,empname) != 0)  ///if the entered record match
            {
                fwrite(&e,recsize,1,ftemp); ///move all records except the one which is to be deleted to temp file
            }
        }

        fclose(fptr);
        fclose(ftemp);
        remove("ems.txt"); ///remove original file
        rename("Temp.dat","ems.txt"); ///rename temp file to original file name
        fptr = fopen("ems.txt", "rb+");
        printf("Delete another record(y/n)");
        int ch;  while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
        next = getche();


    }while( tolower(next) != 'n' );
    fclose(fptr);
    exit(0);
}

【问题讨论】:

  • 你至少在一个地方有它[但是,不是其他一些地方]:scanf("%s",e.sex);其他人使用%c你想要:%s因为e.sexchar *
  • @CraigEstey,轻微的挑剔:e.sex不是char *,即使在这种情况下它确实会导致char *
  • @HAL9000 是的。 e.sexchar 数组——我的错。我现在要喝 [过期的] 下午咖啡 :-)

标签: c types arguments warnings


【解决方案1】:

代码的主要问题是:

scanf("%c",e.sex); 

e.sex 是一个 char 数组,但通过使用 %c 说明符,您应该提供单个 char 变量的地址,即将 char sex[7]; 替换为 char sex;

同理:

 scanf("%s%c%s%s%d%f%d", e.name, e.sex, e.adrs, e.dsgn, &e.age, &e.slry, &e.empID);
          ^^                     ^^^^^

由于您在switch 语句中使用此变量,您可能需要考虑将struct employee 成员变量sex 设为单个char

或者如果它真的是一个你需要的 char 数组:

scanf("%6s",e.sex); //6 char limit for a 7 char container

产生您描述的错误的代码也是如此。

printf("\n\n%s \t\t%c \t%s ... e.sex, e.adrs, e.dsgn, e.age, e.slry, e.empID);
                   ^^          ^^^^^  

在这种情况下,一个简单的%s 说明符就足够了。


但是您不能在switch 中使用e.sex 或任何字符数组。

这导致您在switch(*e.sex) 中取消引用e.sex,这意味着您正在使用e.sex char 数组的第一个字符,如果是故意的,好吧,这是合法的,虽然不常见,但请注意这一点.

【讨论】:

  • @chux-ReinstateMonica,发现和详细。
猜你喜欢
  • 2020-10-18
  • 1970-01-01
  • 2014-04-23
  • 2020-10-18
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多