1

I have a problem with searching in an array of structures. Please help Here is the code I have tried so far:

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

typedef struct{
    long long unsigned num;
    char name[20];
}Telbook;

int be(Telbook*);
void ki(Telbook*);
void search (Telbook*);

int main(){

printf("\t\t\t    \n\n\n");
Telbook tomb[50];
int db;
//Telbook *array=tomb;
db=be(tomb);            
ki(tomb);
search(tomb);
system("pause");
}
int be(Telbook *n){
    int i=0;
    printf("Enter phone # and names until the phone # you entered is 0\n");

    /*printf("Kérek egy nevet: ");
    scanf("%s",n->name);*/
    printf("Enter a Phone #: ");
    scanf("%llu",&n->num);

    while(n->num){
        printf("Enter a name: ");
        scanf("%s",n->name);
        i++;
        n++;
        printf("Enter a phone #: ");
        scanf("%llu",&n->num);

    }
    return i;
}
void ki(Telbook *n){
    int i=0;
    while(n[i]->num){

    printf("Name: %s, Phone #: %llu\n",n[i]->name,n[i]->num);
    i++;
    }
}
void search(Telbook *n){
    int i;
    int db=be(Telbook *n);
    char nev[20];
    printf("Enter the name you're  searching for: ");
    scanf("%s",nev);
    for(i=0;i<db;i++){
        if(n[i].name==nev)break;
        printf("%s",n[i.name]);
    }
    if(i==db){
    printf("The name doesn't exist'.\n");
    }
    else{
        printf("The name you have searhed for is: %s it's on the %d. index.\n",nev,i+1);
    }
}

Like I would like to search for the name in the struct.

bruno
  • 32,421
  • 7
  • 25
  • 37
Ali Dahud
  • 15
  • 1
  • 6

1 Answers1

3

This line to compare two strings will not work:

if(n[i].name==nev)break;

What it does is checking the addresses of the two variables, which are not equal by default.

What you need is strcmp:

if (strcmp(n[i].name, nev) == 0)break;

Here is a documentation page about this this function.

(btw, not tested).

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • Dear Michel, isn't there any kind of alternative? – Ali Dahud Jan 10 '19 at 15:32
  • @AliDahud Curious, Why do you want an alternative to `strcmp(n[i].name, nev) == 0`? – chux - Reinstate Monica Jan 10 '19 at 15:35
  • The strcmp is a very generic library/header file and used for multiple decades; it is the preferred way to compare strings. Of course you also can write your own function that returns an int (as boolean) and either call the strcmp function or write your own for loop. – Michel Keijzers Jan 10 '19 at 16:30
  • @chux I want it because our teacher is very picky and he doesn't agree with us using just using pre-written functions... he made us even write the strcpy and the strlen function once xd – Ali Dahud Jan 10 '19 at 16:51
  • @MichelKeijzers But how shhould I write it then? – Ali Dahud Jan 10 '19 at 17:02
  • In that case write a while loop to check each character separately (although I think gaining knowledge about existing libraries/header files is more important than writing yourself such function). There is probably plenty code enough to write yourself. – Michel Keijzers Jan 10 '19 at 17:02
  • See https://stackoverflow.com/questions/22973670/creating-my-own-strcmp-function-in-c – Michel Keijzers Jan 10 '19 at 17:04
  • @MichelKeijzers that's not exactly what I want. But thanks anyway. I'll ask him about what you said. and about the strcomp thing :D – Ali Dahud Jan 10 '19 at 17:31
  • @AliDahud A sample [`my_strcmp()`](https://stackoverflow.com/a/54136499/2410359). – chux - Reinstate Monica Jan 10 '19 at 20:37