在C中搜索整型*

在C中搜索整型*,c,C,我是编程新手,但我想写一个代码,我可以搜索我员工的“图片”。但是当前的代码不能像我希望的那样工作,因为我只能输入一张图片,然后程序崩溃 typedef struct staff { char id[12]; int *pic; int imagecount; } staff; int main (void) { int employeecount = 0; int pic = 0; int test[3] = { 1, 2, 3 };

我是编程新手,但我想写一个代码,我可以搜索我员工的“图片”。但是当前的代码不能像我希望的那样工作,因为我只能输入一张图片,然后程序崩溃

typedef struct staff {
    char id[12];
    int *pic;
    int imagecount;

} staff;

int main (void)
{
    int employeecount = 0;
    int pic = 0;
    int test[3] = { 1, 2, 3 };

    staff mystaff[100] = { {"111", test, 3}, {"222", test, 3} };

    employeecount = 2;

    printf ("type in a pic you would like to search after\n");
    scanf ("%d", &pic);
    for (int i = 0; i < employeecount; i++) {
        if (strstr (&mystaff[i].pic, pic)) {    //// here im guessing? 
            printf ("%s      ", mystaff[i].id);
            printf ("%d ", mystaff[i].pic);
        }
        printf ("\n");


        return 0;
    }
}
typedef结构人员{
字符id[12];
int*pic;
int图像计数;
}工作人员;
内部主(空)
{
int employeecount=0;
int pic=0;
int-test[3]={1,2,3};
职员mystaff[100]={{“111”,test,3},{“222”,test,3};
雇员人数=2;
printf(“键入您要搜索的图片\n”);
scanf(“%d”和&pic);
对于(int i=0;i

有人知道怎么做吗?是的,它必须是*pic,因为这是我试图做的另一个小程序的一部分

您似乎正在使用一个字符串函数(
strstr()
)来搜索整数,这是行不通的。您需要显式查找整数:

bool staff_has_pic(const staff *s, const int pic)
{
  for (int i = 0; i < s->imagecount; ++i)
  {
    if (s->pic[i] == pic)
      return true;
  }
  return false;
}
此外,请确定获取号码的方式:

if (scanf(" %d", &pic) != 1)
{
  printf("**Failed to get picture number\n");
  exit(1);
}

函数
strstr
如下所示:

char* strstr (const char* str1, const char* str2);
你打电话:

if (strstr (int **pic1, int pic2) != NULL) {}

在第一个格兰斯,
scanf(“%d”,pic)
错了。
“///我在猜?”
提示:永远不要用C猜。查一下。这将为你节省数小时的时间和沮丧。这就是手册页的用途。
strstr
仅用于以null结尾的字符*
if (strstr (int **pic1, int pic2) != NULL) {}