#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUCKETS 11
typedef struct {
char item[10];
int key;
}element;
element ht[BUCKETS];
void p1();
unsigned int stringToInt(char *key);
int h(int k);
void insert(char *str);
int main() {
p1();
return 0;
}
void p1() {
FILE* f = fopen("input.txt", "r");
if (!f) {
fprintf(stderr, "file doesn't exist\n");
exit(EXIT_FAILURE);
}
char line[200];
fgets(line, sizeof(line), f);
printf(" input strings : %s\n\n", line);
char delimeter[5] = " ";
char* word = strtok(line, delimeter);
insert(word);
while (word) {
word = strtok(NULL, delimeter);
printf("%s\n", word);
insert(word);
}
printf(" item key\n");
}
unsigned int stringToInt(char *key) {
int number = 0;
while (*key)
number += *key++;
return number;
}
int h(int k) {
return k % BUCKETS;
}
void insert(char *str) {
int key = stringToInt(str);
int hk = h(key);
int i = hk;
while (&ht[i])
i++;
strcmp(ht[i].item, str);
ht[i].key = key;
}
아직 코드 다짠건 아닌데 코드 문법오류는 없는데 디버깅해보니까 insert함수에서 strcmp할때
예외 발생(0x7B54EA10(ucrtbased.dll), 1.exe): 0xC0000005: 0x00000000 위치를 읽는 동안 액세스 위반이 발생했습니다..
요런오류뜨는데 왜그런것임??
인풋 텍스트파일은 그냥 문자열 나열되있음 absd sdfga gargrg gadfdfg 이런식으로
while (&ht[i]) i++;
그거 &빼면 식에 산술 또는 포인터형식이 있어야합니다 라고 문법오류나옴..
ht 구조체의 내용을 확인해야지
어 while(hk[i].key) 로 하니까 스트링 출력이 다되긴했는데 그다음에 (null)출력되고 비정상종료되네
아 알았다 땡큐땡큐