-1

I've an assigment about hashes. And i've finished my work. At least i thought so... Anyway My duty is creating a structure and indexing it with a hash table. I've done all the functions etc. but when i want to take some imput to my struct exe file stoppes working and finishes the program. So what is the reason of that?

Here's my code:


#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct {
    int musteri_no;
    char ad[20], soyad[20];
} Musteri;
Musteri *veri_liste[10];
int hash_tablo[10][2];

//Hash tablosuna istenilen şekilde veri koyan fonksiyon
void put(int musteri_no,Musteri* musteri){
    printf("Calisii");
    while(true){
        if(hash_tablo[musteri_no%10][0]==-1){
            hash_tablo[musteri_no%10][0]=musteri_no;
            veri_liste[musteri_no%10]=musteri;
            printf("Calisii");
        }
        else{
            for(int i=0;i<10;i++){
                if(hash_tablo[i][1]!=-1){
                    continue;
                    printf("Calisii");
                }
                else{
                    printf("Calisii");
                    hash_tablo[musteri_no%10][1]=i;
                    hash_tablo[i][0]=musteri_no;
                    
                }
            }
        }
    }
}
void search(int musteri_no){
    int i=1,kontrol=hash_tablo[musteri_no%10][1];
    printf("%d : Aranilan deger",musteri_no);
    while(i<=1){
        if(hash_tablo[musteri_no%10][0] == musteri_no){
            printf("%d %s %s",veri_liste[musteri_no%10]->musteri_no,veri_liste[musteri_no%10]->ad,veri_liste[musteri_no%10]->soyad);
            printf("%d Adimda bulundu",i);
            break;
        }
        else{
            if(i==1){
                i++;
                if(hash_tablo[kontrol][0]==musteri_no){
                    printf("%d %s %s",veri_liste[kontrol]->musteri_no,veri_liste[kontrol]->ad,veri_liste[kontrol]->soyad);
                    printf("%d Adimda bulundu",i);
                    break;
                }
                else{
                    kontrol=hash_tablo[kontrol][1];
                }
            }
            else{
                i++;
                if(hash_tablo[kontrol][0]==musteri_no){
                    printf("%d %s %s",veri_liste[kontrol]->musteri_no,veri_liste[kontrol]->ad,veri_liste[kontrol]->soyad);
                    printf("%d Adimda bulundu",i);
                    break;
                }
                else{
                    kontrol=hash_tablo[kontrol][1];
                }
            }
        }
    }
}
void delete_value(int musteri_no){
    for(int i=0;i<10;i++){
        if(hash_tablo[i][0]==musteri_no){
            hash_tablo[i][0]=-1;
        }
        else{
            continue;
        }
    }
}
void hash_list(){
    for(int i=0;i<10;i++){
            printf("%d %d\n",hash_tablo[i][0],hash_tablo[i][1]);
    }
                            
}
void data_list(){
    for(int i=0;i<10;i++){
        if(veri_liste[i]==NULL){
            continue;
        }
        else{
            printf("%d %s %s\n",veri_liste[i]->musteri_no,veri_liste[i]->ad,veri_liste[i]->soyad);
        }
    }
}
int main(){
    Musteri* gecici_m;
    char isim[20],soyisim[20];
    int musteri_no;
    for(int i=0;i<10;i++){
        for(int j=0;j<2;j++){
            hash_tablo[i][j]=-1;
        }
    }
    printf("Musteri no giriniz:");
    scanf("%d",&musteri_no);
    printf("İsim giriniz:");
    scanf("%s",&isim);
    printf("Soyisim giriniz:");
    scanf("%s",&soyisim);
    strcpy(gecici_m->ad,isim);
    strcpy(gecici_m->soyad,soyisim);
    gecici_m->musteri_no=musteri_no;
    put(musteri_no,gecici_m);
    printf("Hash list:\n");
    hash_list();
    printf("Data list:\n");
    data_list();`
}

I'll give all the code for you because i can't figure out what's wrong with here. Thanks a lot.

Kaan Deniz
  • 23
  • 1
  • 5
  • 1
    `Musteri* gecici_m;` That pointer is never initialized. – dxiv Dec 26 '20 at 01:12
  • How can i do that? When i'm trying to do this i get an error like: Invalid conversion from 'void*' to 'Musteri*'[-fpermissive] ---- And The line that i'm trying to allocating is : Musteri *veri_liste=malloc(10*sizeof (Musteri)) – Kaan Deniz Dec 26 '20 at 11:53
  • You are trying to allocate the wrong pointer there. Try `Musteri *gecici_m = malloc(sizeof (Musteri));` in C, or `Musteri *gecici_m = (Musteri *)malloc(sizeof (Musteri));` in C++. – dxiv Dec 26 '20 at 17:14

1 Answers1

2

Other than using unallocated pointer which is already pointed by @dxiv, you have following errors from your code.

1)

main.c: In function ‘put’:
main.c:23:11: error: ‘true’ undeclared (first use in this function)
while(true){
^~~~ main.c:23:11: note: each undeclared identifier is reported only once for each function it appears in
main.c: In function ‘main’:
main.c:118:13: warning: format ‘%s’ expects argument of type ‘char * ’, but argument 2 has type ‘char ( * )[20]’ [-Wformat=]
scanf("%s",&isim);
^ main.c:120:13: warning: format ‘%s’ expects argument of type ‘char * ’, but argument 2 has type ‘char ( * )[20]’ [-Wformat=]
scanf("%s",&soyisim);
^ main.c:128:17: error: stray ‘’ in program data_list(); ^

There are two warning complaining about the types you used %s expects argument of type ‘char * ’, but argument 2 has type ‘char ( * )[20]

you have to fix those as below.

scanf("%s",isim);
scanf("%s",soyisim);

The others looks like some typo and hidden code, please correct them.

There are other problematic areas in your program.

  1. You also need to allocate memory for Musteri *veri_liste[10]; before using, otherwise your program behavior is undefined.

  2. This function of yours void put(int musteri_no,Musteri* musteri) is running in while(true) and you have not handled any termination point for it, without that your code will run into infinite loop.

IrAM
  • 1,720
  • 5
  • 18
  • I've an another question too than.. How can i allocate memory for my struct array? – Kaan Deniz Dec 26 '20 at 11:37
  • 1
    There are good examples [1](https://stackoverflow.com/questions/12854326/malloc-with-structs-in-c) [2](https://stackoverflow.com/questions/14768230/malloc-for-struct-and-pointer-in-c) out there on SO, have a look at them and post a new question if you still have any. – IrAM Dec 26 '20 at 13:13