В настоящее время я получаю странную ошибку при попытке скомпилировать мой код на C: при создании экземпляра и назначении переменной компилятор сообщает об ошибке, что параметр уже инициализирован:
tasks.c: In function ‘hashfunc’:
tasks.c:7:1: error: parameter ‘DESIRED_HASH’ is initialized
char* DESIRED_HASH = "e65493ccdee9c4514fe20e0404f3bcb8";
Для строки 9, Я получаю: ошибка: класс хранения, указанный для параметра "word_entry"
Мой код:
#include "md5.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hash.h"
const char* DESIRED_HASH = "e65493ccdee9c4514fe20e0404f3bcb8";
typedef struct word_entry {
char* word;
word_entry* next_word;
}word_entry;
typedef struct result {
char* a;
char* b;
}result;
int word_count = 0;
void add_word(word_entry* head, char* new_word)
{
word_entry* entry = head;
while(entry->next_word != NULL)
{
if(strcmp(entry->word, new_word) == 0)
{
return;
}
entry = entry->next_word;
}
word_entry* new_entry = malloc(sizeof(word_entry));
new_entry->word = new_word;
new_entry->next_word = NULL;
entry->next_word = new_entry;
word_count++;
}
char* get_word(word_entry* head, int index)
{
word_entry* curr = head;
for(int i = 0; i < index; i++)
{
curr = curr->next_word;
}
return curr;
}
int main(){
char* words = "das sind die woerter ( ginge auch als methoden parameter )";
word_entry* head = NULL;
char* tok = strtok(words, " ");
head = malloc(sizeof(word_entry));
head->word = tok;
head->next_word = NULL;
tok = strtok(NULL," .,;-:0123456789?!\"*+()|&[]#$/%%’");
while(tok != NULL)
{
add_word(head, tok);
tok = strtok(NULL," .,;-:0123456789?!\"*+()|&[]#$/%%’");
}
printf("%d words\n", word_count);
char** pWords = malloc(sizeof(char*) * word_count);
word_entry* entry = head;
for(int i = 0; i < word_count; i++)
{
pWords[i] = entry->word;
entry = entry->next_word;
}
for(int i = 0; i < word_count; i++)
{
for(int j = 0; j < word_count; j++)
{
char* first_word = pWords[i]; // oder get_word(i)
char* second_word = pWords[j]; // oder get_word(j)
char* result = hashfunc(first_word,second_word);
int res = strcmp(result,DESIRED_HASH);
if(res==0){
printf("%s and %s lead to the hash",first_word,second_word);
}
}
return 0;
}
В чем здесь может быть ошибка?Я был бы благодарен за любую помощь, поскольку я в настоящее время застрял здесь.Я подозреваю синтаксическую ошибку, но не уверен.
Заранее спасибо.
PS: "Hash.h" состоит из:
extern char* hashfunc (char* word1, char* word2)
"Hash.c":
#include "md5.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char* hashfunc (char* word1, char* word2){
MD5_CTX md5;
MD5_Init(&md5);
char * word = (char *) malloc(strlen(word1)+ strlen(word2) +1);
strcpy(word,word1);
strcat(word,word2);
MD5_Update(&md5,word,strlen(word));
unsigned char* digest = malloc(1+ (sizeof(char)* 16));
MD5_Final(digest,&md5);
char* str = malloc(32*sizeof(char));
for (int i = 0; i < 16; i++){
sprintf(str+2*i, "%02x", (int)(unsigned char)digest[i]);
}
free(word);
free(digest);
return str;
}