为了完成一个更大的项目,我试图了解如何将结构数组和 char* 类型的标记发送到函数。这段代码的目的是执行以下操作:
- 打开文件
- 标记文件
- 将 token 和结构数组发送到搜索函数
- 搜索功能将遍历结构数组,使用 strcmp 找到与 token 匹配的项
- 如果找到匹配返回 1,主函数将检查 1 或 0
- 如果为 0,则不向结构数组添加标记,如果为 1,则向结构数组添加标记
我刚刚写了一个小程序,看看我是否可以将数组和 token 发送到一个函数并进行比较,但是我遇到了很多错误,我不知道该怎么做,因为我不理解大多数错误。
#include <stdio.h>
#include <string.h>
int search(struct id array[],char* tok);
struct id
{
char name[20];
int age;
};
int main(void)
{
struct id person[2] = { {"John Smith", 25},
{"Mary Jones", 32} };
char* token = "Mary Jones"; /*char* for strtok() return type*/
search(person,token);
}
int search(struct id array[],char* tok)
{
int i,value;int size = 2;
for(i=0;i<size;i++)
{
if(strcmp(array[i].name,tok) == 0)
value = 0;
else
value = 1;
}
return value;
}
最佳答案
地点
int search(struct id array[],char* tok);
在 struct
声明之后。并将 search
的返回值赋给一个 int
变量。
int found = search(person,token);
if(found == 0)
printf("Name is found\n"); // or whatever you want
关于c - 发送结构数组到函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24640899/