Compilation error in C error: conflicting types for \'ht_insert\' below are my m
ID: 3762165 • Letter: C
Question
Compilation error in C
error: conflicting types for 'ht_insert'
below are my main and ht_insert functions and the error is occurring on the line of the function itelf. What is causing it?
unsigned int hash(const char *); // function prototypes
Node **ht_create(void);
Node **ht_create_withSize(int);
int ht_insert(Node **, char *);
void ht_print(Node **);
void ht_destroy(Node **);
int htsize;
int main(int argc, char *argv[])
{
Node **Table; // hash table
char *delim = " .,;:!"? "; // delimiter characters for strtok()
char *token; // token returned by strtok()
char *line = NULL; // Address of buffer containing text for getline()
size_t length = 0; // Set to allocate buffer for storing line
// create the hash table
Table = htcreate();
// set the size of the hash table to argv's int value
htsize = atoi(argv[1]);
while (1) {
if (getline(&line, &length, stdin) == -1) // read next line
break; // exit loop when no more lines
token = strtok(line, delim); // extract next token from line
while (token != NULL) {
ht_insert(Table, token);
token = strtok(NULL, delim); // extract next token
}
}
free(line); // free line buffer
ht_print(Table); // print hash table
ht_destroy(Table); // empty hash table
return 0;
}
int ht_insert(Node **Table, const char *word)
{
Node *prevp, *p;
assert(Table != NULL);
if (p == NULL)
return 0;
p->next = NULL;
if (Table[word] == NULL)
Table[word] = p;
else
{
prevp = Table[word];
while (prevp->next != NULL)
{
prevp = prevp->next;
}
prevp->next = p;
}
return 1;
}
Explanation / Answer
In C, Node *n is incorrect, we should use struct Node *n. I have replaced it with Node. Check now.
#include<stdio.h>
struct Node{
int data;
struct Node *next;
};
unsigned int hash(const char *); // function prototypes
struct Node **ht_create(void);
struct Node **ht_create_withSize(int);
int ht_insert(struct Node **, char *);
void ht_print(struct Node **);
void ht_destroy(struct Node **);
int htsize;
int main(int argc, char *argv[])
{
struct Node **Table; // hash table
char *delim = " .,;:!"? "; // delimiter characters for strtok()
char *token; // token returned by strtok()
char *line = NULL; // Address of buffer containing text for getline()
size_t length = 0; // Set to allocate buffer for storing line
// create the hash table
Table = htcreate();
// set the size of the hash table to argv's int value
htsize = atoi(argv[1]);
while (1) {
if (getline(&line, &length, stdin) == -1) // read next line
break; // exit loop when no more lines
token = strtok(line, delim); // extract next token from line
while (token != NULL) {
ht_insert(Table, token);
token = strtok(NULL, delim); // extract next token
}
}
free(line); // free line buffer
ht_print(Table); // print hash table
ht_destroy(Table); // empty hash table
return 0;
}
int ht_insert(struct Node** Table, const char *word)
{
struct Node *prevp, *p;
assert(Table != NULL);
if (p == NULL)
return 0;
p->next = NULL;
if (Table[word] == NULL)
Table[word] = p;
else
{
prevp = Table[word];
while (prevp->next != NULL)
{
prevp = prevp->next;
}
prevp->next = p;
}
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.