我需要编写一个计数排序算法,它按字母顺序排序字符串,但我在Count函数中得到“错误:数组下标不是整数”。可能需要使用ASCII,但我不知道如何。任何想法?
aaa.c: In function ‘Count’:
aaa.c:20:23: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
C[i]=(char)((int)C[i]+(int)C[i-1]);
^
aaa.c:20:33: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
C[i]=(char)((int)C[i]+(int)C[i-1]);
^
aaa.c:20:15: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
C[i]=(char)((int)C[i]+(int)C[i-1]);
^
aaa.c:22:25: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
B[(int)C[(int)A[j]]]=A[j];
^
aaa.c:22:18: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
B[(int)C[(int)A[j]]]=A[j];
^
aaa.c:23:18: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
C[(int)A[j]]=C[(int)A[j]]-1;
^
aaa.c:23:31: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
C[(int)A[j]]=C[(int)A[j]]-1;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MaxStringLength 30
#define MaxStringNumber 5
#define NumberOfLetters 26
char **A;
char **B;
char **C;
void Count(char **A, char **B, int quantity){
int i,j;
for (i=1;i<=NumberOfLetters;i++) C[i]=0;
for (j=1;j<=quantity;j++)
C[A[j][1]-96] +=1;
for (i=2;i<=NumberOfLetters;i++)
C[i]=(char)((int)C[i]+(int)C[i-1]);
for (j=MaxStringNumber;j>0;j--){
B[(int)C[(int)A[j]]]=A[j];
C[(int)A[j]]=C[(int)A[j]]-1;
}
}
void printing(char **array, int quantity){
int i;
for (i=1;i<=quantity;i++)
printf("%s \n", array[i]);
}
void reading(char **array, int quantity){
char word[MaxStringLength];
int i;
for (i=1;i<=quantity; i++){
scanf("%s",word);
array[i] = (char*) malloc(sizeof(char)*MaxStringLength);
strcpy(array[i],word);
}
}
int main(){
A=(char**) malloc((MaxStringNumber+1)*sizeof(char*));
B=(char**) malloc((MaxStringNumber+1)*sizeof(char*));
C=(char**) malloc((MaxStringNumber+1)*sizeof(char*));
printf("Array A:\n");
reading(A,MaxStringNumber);
Count(A,B,MaxStringNumber);
printf("Sorted:\n");
printing(B,MaxStringNumber);
return 0;
}
答案 0 :(得分:0)
eng.c:18:18: error: array subscript is not an integer
C[A[j]] = C[A[j]]+1;
错误消息表示它所说的内容。
A is a
字符**`
A[j]
为char *
您无法将char *
用作数组索引。
很难知道如何纠正它,因为A,B,C不是好的变量名。也许你的意思是(int)(*A[j])
表示字符串中第一个字符的int值,但似乎不太可能。第一步,将A,B,C的名称更改为有意义的名称。