c语言结构体应用二

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define M 100

struct data
{
    int id;
    char name[20];
    double price;
}book[M];

//在有M个元素的结构体数组s中查找名为name的书,若找到,函数返回数组下标,否则,函数返回-1

int findbook(struct data s[], char name[])
{
    int i;
    for( i = 0;i<M;i++)
       if(strcmp(s[i].name,name) == 0 )
       return i;
    return -1;
}

int main()
{
    strcpy(book[2].name,"空间几何");

    printf(" i is %d ",findbook(book,"空间几何"));

    return 0;
}






一勤天下无难事。
原文地址:https://www.cnblogs.com/nowroot/p/15366409.html