关于指针:以下 C 代码显示: format \\’%d\\’ 需要类型为 \\’int *\\’ 的参数,但参数 7 的类型为 float *

The following C code shows that : format '%d' expects argument of type 'int *', but argument 7 has type float *

以下代码显示:格式\\'%d\\' 需要类型为\\'int *\\' 的参数,但参数7 的类型为float *。我不是专家,但我无法区分错误。这个问题出现在 scanf 中。除此问题外,还有 3 个相关警告。它位于 void edit () 部分的第 158 行。我一直在尝试并得到同样的东西。请问有人可以帮忙吗?

问题:

1
2
3
4
5
6
7
8
9
if(strcmp(e.name,empname) == 0)
            {
                printf("\
Enter new name,sex,address,designation,age,salary,employee ID"
);
                scanf("%s %s %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,&e.age,&e.slry,&e.empID);
                fseek(fptr,-recsize,SEEK_CUR);
                fwrite(&e,recsize,1,fptr);
                break;
            }

这里的结构:

1
2
3
4
5
6
7
8
9
struct employee
{
        char name[50];
        char sex;
        char adrs[50];
        char dsgn[25];
        int age,empID;
        float slry;
};

完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#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 ext();

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



int main()
{
    //FILE * fptr, *ft;
    int choice;
    //fptr = fopen("ems.txt","rb+");

    fptr = fopen("ems.txt","r+");


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

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

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


    while(1)
    {
        printf("*******************************\
"
);
        printf("\
Employee management system"
);
        printf("\
1. Insert employee information"
);
        printf("\
2. List all employee information"
);
        printf("\
3. Edit employee information"
);
        printf("\
4. Delete employee information"
);
        printf("\
5. Exit"
);
        printf("\
\
*****************************\
"
);
        printf("\
\
Enter your choice:"
);
        scanf("%d", &choice);
        fflush(stdin);

        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");
                ext(1);
                break;
            default:
                puts("Choice is incorrect!!");
                continue;
        }
    }

    return 0;
}


void insert()
{
    char next;

    do
    {
        printf("********************************************************** \
"
);
        printf("\
Enter the name of the employee:"
);
        fgets(e.name);
        printf("\
Enter the sex of the employee (M/m or F/f):"
);
        fgets(&e.sex);
        printf("\
Enter the address of the employee:"
);
        fgets(e.adrs);
        printf("\
Enter designation of the employee:"
);
        fgets(e.dsgn);
        printf("\
Enter age of the employee:"
);
        scanf("%d", &e.age);
        printf("\
Enter basic salary of the employee:"
);
        scanf("%f", &e.slry);
        printf("\
Enter 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 \
%f \
%d \
"
, e.age, e.slry, e.empID);
       // fwrite(&e,recsize,1,fptr);
        fflush(stdin);
        printf("\
Do you want to input more? (y/n):"
);
        next = getche();
        printf("\
"
);


    }
    while(next !='n');

    fclose(fptr);
}

void list ()
{
    /* what is going on here??? */
    while(fread(&e,recsize,1,fptr)==1)
    {
        printf("\
%s %c %s %s %d %.2f %d"
,e.name,e.sex,e.adrs,e.dsgn,e.age,e.slry,e.empID);
    }

    getche();
    return ;
}

void edit ()
{
    char next;
    do
    {
        printf("Enter the employee name to be edited:");
        scanf("%s", empname);
        while(fread(&e,recsize,1,fptr)==1)
        {
            if(strcmp(e.name,empname) == 0)
            {
                printf("\
Enter new name,sex,address,designation,age,salary,employee ID"
);
                scanf("%s %s %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,&e.age,&e.slry,&e.empID);
                fseek(fptr,-recsize,SEEK_CUR);
                fwrite(&e,recsize,1,fptr);
                break;
            }
        }
        printf("\
Edit another record(y/n)"
);
        next = getche();
        fflush(stdin);

    }
    while(next != 'n');


    return ;
}

void del()
{
    char next;
    do
    {
        printf("\
Enter name of employee to delete:"
);
        scanf("%s",empname);
        ftemp = fopen("Temp.dat","wb");
        while(fread(&e,recsize,1,fptr) == 1)
        {
            if(strcmp(e.name,empname) != 0)
            {
                fwrite(&e,recsize,1,ftemp);
            }
        }

        fclose(fptr);
        fclose(ftemp);
        remove("ems.txt");
        rename("Temp.dat","ems.txt");
        fptr = fopen("ems.txt","rb+");
        printf("Delete another record(y/n)");
        fflush(stdin);
        next = getche();


    }while(next !='n');
}


如果您向我们展示您在该行遇到的所有错误,那就太好了:

1
2
3
4
5
6
7
x1.c: In function ‘edit’:
x1.c:170:17: warning: format ‘%s’ expects argument of type ‘char *, but argument 3 has type ‘int[-Wformat=]
                 scanf("%s %s %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,&e.age,&e.slry,&e.empID);
                 ^
x1.c:170:17: warning: unknown conversion type character ‘.’ in format [-Wformat=]
x1.c:170:17: warning: format ‘%d’ expects argument of type ‘int *, but argument 7 has type ‘float *[-Wformat=]
x1.c:170:17: warning: too many arguments for format [-Wformat-extra-args]

第一个警告可以追溯到您之前的问题。 e.sex 具有 char 类型(被提升为 int),但您指定了需要 char *%s。要读取此字段,您要使用 %c 格式说明符,它读取单个字符而不是字符序列,并且您要传递要读取的字段的地址,即 &e.sex.

第二个警告是由于您使用 %.2f 作为格式说明符。与 printf 不同,scanf 不采用精度。将此更改为 %f。一旦你这样做了,第三个错误就会消失。

通常,始终从上到下解决编译器错误,因为代码中早期的问题可能会向下级联。