1
我是C編程語言的初學者。我想寫一個哈希程序。我可以使用有限數量的typedef名稱來執行此程序,但是當我使用動態分配時,會出現無效的初始化程序錯誤。無效初始化程序 - C
typedef char Name[30];
Name hashTable[MAX];
int hash(Name name){
int long sum = 0;
int len=strlen(name);
int i = 0;
for (; i<len;i++)
sum += name[i];
sum = sum % MAX;
printf("\nhash of [%s] = %ld\n",name,sum);
return sum;
}
void main(){
int i,j;
for(i=0;i<MAX;i++)
strcpy(hashTable[i],"");
int pos, x, cont=1;
printf("number of names: ");
scanf("%d",&x);
while (x>=cont){
Name name = malloc(sizeof(Name)); // why this line have the error of "invalid initializer"?
printf("\ntype the %dº name: ",cont);
scanf("%s",name);
pos=hash(name);
strcpy(hashTable[pos],name);
cont++;
}