the problem is that when I try to copy the name from "Buffer" to the "Node" using "sprintf" I always get "segmentation fault" !!.
THE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <stdint.h>
#include <string.h>
//node declaration:
typedef struct node
{
char* name[20];
struct node* link;
}
node;
unsigned int hash(char* buffer ,unsigned int CAPACITY) ;
int main(int argc , string argv[])
{
//declarations:
FILE* fp = fopen("us-names.txt" , "a+") ;
if(fp == NULL)
{
printf("CANNOT OPEN THE FILE !\n") ;
return 1 ;
}
char buffer[20] ;
unsigned int key = 0;
unsigned int CAPACITY = 100 ;
struct node* temp = NULL ;
struct node* hashtable[CAPACITY] ;
//Import name from the FILE & store into buffer:
fgets(buffer , 20 , fp) ;
//pass the name into the hash_function and get the key:
key = hash(buffer , CAPACITY) ;
//put the name in a node then in it's right place in the hashtable using the key:
temp = malloc(sizeof(struct node)) ;
sprintf(*temp->name , "%s" , buffer) ;
hashtable[key] = temp ;
//close the FILE & return 0:
fclose(fp) ;
return 0;
}
unsigned int hash(char* buffer , unsigned int CAPACITY)
{
unsigned int sum = 0 ;
for(int i = 0; buffer[i] != '\0' ;i++)
{
sum += buffer[i] ;
}
return sum % CAPACITY ;
}
when I debug my program it shows me that the problem is at this line : sprintf(*temp->name , "%s" , buffer) ;
and I've tried too many ways to fix it put all of them didn't work !!
I obviously don't know what to do ,I hope some one can help :(