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 |
这里的结构:
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(); FILE * fptr, *ftemp; int main() fptr = fopen(“ems.txt”,“r+”);
if (fptr == NULL) fptr = fopen(“ems.txt”,“w+”); //Explain the reason for this?
while(1) switch(choice) break; return 0;
void insert() do
} fclose(fptr); void list () getche(); void edit () }
return ; void del() fclose(fptr);
}while(next !=‘n’); |
- 这与 2 小时前您之前的问题非常相似:stackoverflow.com/questions/62625213/…
- 这回答了你的问题了吗?以下 C 代码显示:格式 ‘%s’ 需要类型为 ‘char *’ 的参数,但参数 3 的类型为 int
- 感谢您的建议,但问题有点不同。正如其他人所提到的,”scanf”并不精确,即”
如果您向我们展示您在该行遇到的所有错误,那就太好了:
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。一旦你这样做了,第三个错误就会消失。
通常,始终从上到下解决编译器错误,因为代码中早期的问题可能会向下级联。
- 谢谢你。我不再有与类型相关的警告。但是现在 fgets 错误
- @DDArtCustom 查找您应该如何调用该函数。
- 谢谢, fgets 函数有三个参数。
来源:https://www.codenong.com/62627136/