【发布时间】:2016-12-18 12:27:57
【问题描述】:
我有 2 个变量。一个应该是unsigned short array[10] 类型(我被要求这样做),另一个是来自scanf 的输入,方便作为字符串。
我将值一一传递,从字符串到带有 for 循环的无符号 short。
程序运行正常,但我收到警告消息。"Warning passing argument of strcmp from incobatible pointer type"
和"expected const char * but argument is of type short unsigned int"。
我似乎无法找到如何在没有警告的情况下从字符串传递到 unsigned short。
我的程序的简短版本在这里,以便您有更清晰的视图。
警告位于第 28,43 行。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int i, j, numberofseats;
char phone[11];
char *p;
unsigned short tempphone[10];
typedef struct
{
char fullname[40];
unsigned short phonenr[10];
unsigned int seatnr;
} PASSENGERS;
int main()
{
PASSENGERS passenger[53];
printf("Enter Passenger's Phone Nr:");
scanf("%s", phone);
i = 0;
for (p = phone; *p != '\0'; p++)
{
(tempphone[i]) = *p - '0';
i++;
}
for (j = 0; j < numberofseats; j++)
{
if (strcmp(tempphone, passenger[j].phonenr) == 0)
printf("Passenger %s has Seat Nr %u already Booked", passenger[j].fullname, passenger[j].seatnr);
}
}
【问题讨论】:
-
您想在某处存储
tempphone中实际使用了多少位。目前,您似乎假设 all,但您只初始化了第一个strlen(phone)元素。此外,您不能使用strcmp来比较整数数组。 -
@tofro: "你想在某处存储多少位数" OP 可能会在比较实际 起作用 的那一刻知道这一点。 .. ;-)
-
程序运行良好.. 通过更改一行 -> memcmp(tempphone,passenger[j].phonenr,sizeof(tempphone))==0) 两个警告似乎都消失了..
-
这是在带有 unicode 项目的 Windows 环境中吗?这可能是您被要求这样做的原因。如果是,请使用 tcslen 和 tcscmp 等 unicode 例程代替 strlen 和 strcmp。
-
是的,是的,它是带有 unicode 的窗口.. 也会检查这个!感谢大家!现在我把它留给 memcmp