gccでのコンパイル時に表示されるエラーメッセージの意味がわからない
現在ハッシュテーブルを使い、人物の検索をかければその人の年代が出てくるコードを書いています。下記の三つのソースコードをターミナルでgcc -c hash.c
、gcc -c openaddr.c
、gcc -c main.c
とかけたところ、エラーが出てしまいました。なぜこのエラーメッセージが出てきしまうのかがわかりません。
エラーメッセージ
openaddr.c:30:14: warning: implicit declaration of function 'hash' is invalid in
C99 [-Wimplicit-function-declaration]
int hs = hash(id1);
^
openaddr.c:86:34: warning: more '%' conversions than data arguments [-Wformat]
printf("id: %s, info: %d\n");}
~^
openaddr.c:30:14: warning: implicit declaration of function 'hash' is invalid in
C99 [-Wimplicit-function-declaration]
int hs = hash(id1);
^
openaddr.c:86:34: warning: more '%' conversions than data arguments [-Wformat]
printf("id: %s, info: %d\n");}
~^
2 warnings generated.
main.c:9:5: warning: implicit declaration of function 'initialize' is invalid in
C99 [-Wimplicit-function-declaration]
initialize();
^
main.c:10:5: warning: implicit declaration of function 'enter' is invalid in C99
[-Wimplicit-function-declaration]
enter("Copernicus", 1473);
^
main.c:11:40: warning: implicit declaration of function 'hash' is invalid in C99
[-Wimplicit-function-declaration]
printf("enter Copernicus at %d\n", hash("Copernicus"));
^
main.c:22:5: warning: implicit declaration of function 'show_table' is invalid
in C99 [-Wimplicit-function-declaration]
show_table();
^
main.c:24:9: warning: implicit declaration of function 'search' is invalid in
C99 [-Wimplicit-function-declaration]
t = search(key);
^
ソースコード
#include <stdio.h>
#include <string.h>
#define M 257
#define NEXT_HASH(x) (x + 1) % M
int hash(char *v){
int x=0;
while (*v) x= 256*x + (*v++);
if (x < 0) x = (-x);
return (x % M);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 257
#define NEXT_HASH(x) (x + 1) % M
struct item {
char id[20];
int info;
};
struct item table[M];
void initialize() {
int i;
for (i = 0; i < M; i++)
memset(table[i].id, '\0', sizeof(table[i].id));
}
void show_table() {
int i;
for (i = 0; i < M; i++)
if (strcmp(table[i].id, "") != 0)
printf("[%3d] id: %20s, info: %d\n", i, table[i].id, table[i].info);
}
int enter(char *id1, int info1) {
int i;
int hs = hash(id1);
for (i = 0; i < M; i++) {
if (!strcmp(table[hs].id, "")) {
strcpy(table[hs].id, id1);
table[hs].info = info1;
return 0;
}
hs = NEXT_HASH(hs);
}
return -1;
}
int search(char *id1) {
int hs = hash(id1);
int next = hs;
while(hs == hash(table[next].id)) {
if (!strcmp(table[next].id, id1))
return next;
next = NEXT_HASH(next);
}
return -1;
}
int main(int argc, char *argv[]) {
int qflag = 1;
char str[20];
int n, cmd;
int cancel();
int hashcode =search(str);
while(qflag) {
printf("1.. INSERT 2.. CANCEL 3.. SEARCH 4.. LIST 0..EXIT\n");
scanf("%d", &cmd);
switch(cmd) {
case 1:
printf("id >> ");
scanf("%s", str);
printf("info >> ");
scanf("%d", &n);
enter(str, n);
break;
case 2:
printf("id >> ");
scanf("%s", str);
//if(!strcmp(str, "%s"))
//memset(str, '0', sizeof(str));
//printf("%s canceled\n", str);
break;
case 3:
printf("id >> ");
scanf("%s", str);
if (hashcode ==0){
printf("%s fonded\n", str);
printf("id: %s, info: %d\n");}
else if (hashcode != 0){
printf("%s not found\n", str);}
break;
case 4:
show_table();
break;
case 0:
qflag = 0;
break;
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define M 257
int main(){
int t;
char key[20];
initialize();
enter("Copernicus", 1473);
printf("enter Copernicus at %d\n", hash("Copernicus"));
enter("Galilei", 1564);
printf("enter Galilei at %d\n",hash("Galilei"));
enter("Newton", 1643);
printf("enter Newton at %d\n",hash("Newton"));
enter("Maxwell", 1831);
printf("enter Maxwell at %d\n",hash("Maxwell"));
enter("Einstein", 1879);
printf("enter Einstein at %d\n",hash("Einstein"));
enter("Heisenberg", 1901);
printf("enter Heisenberg at %d\n",hash("Heisenberg"));
show_table();
strcpy(key, "Heisenberg");
t = search(key);
printf("%s => %d\n", key, t);
return 0;
}