我试图对字符串数组执行插入排序。 该数组被格式化为指向char数组的指针数组。
使用以下语句声明数组:
char *wordlist[ARRAY_LEN];
使用:
传递给insertion_sort函数insertion_sort(wordlist, num_words);
insertion_sort的功能如下所示。
void insertion_sort(char **a, int n) {
char *key;
int i, p;
for(p=1; p<n; p++){ /*For every item p in the array apart from the first*/
key = *a[p]; /*Set key to the value of p*/
i = p-1;
while(strcmp(*a[i], key) > 0){ /*For every item to the left of p that is larger*/
*a[i+1] = *a[i]; /*than it move it 1 to the right*/
i--;
}
*a[i+1] = key; /*Place key in the vacated leftmost position*/
}
}
我在编译时不断收到与指针相关的错误/警告:
word-sort.c: In function ‘insertion_sort’:
word-sort.c:21:11: warning: assignment makes pointer from integer without a cast
word-sort.c:23:7: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast
/usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:7: warning: passing argument 1 of ‘__builtin_strcmp’ makes pointer from integer without a cast
word-sort.c:23:7: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:7: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast
/usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:23:7: warning: passing argument 1 of ‘__builtin_strcmp’ makes pointer from integer without a cast
word-sort.c:23:7: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:7: warning: passing argument 1 of ‘__builtin_strcmp’ makes pointer from integer without a cast
word-sort.c:23:7: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:23:13: warning: cast to pointer from integer of different size
word-sort.c:23:7: warning: passing argument 1 of ‘__builtin_strcmp’ makes pointer from integer without a cast
word-sort.c:23:7: note: expected ‘const char *’ but argument is of type ‘char’
word-sort.c:27:15: warning: assignment makes integer from pointer without a cast
任何关于我出错的地方的想法都会很棒。
答案 0 :(得分:2)
看看这段代码:
key = *a[p];
此处key
为char*
,a
为char**
。这意味着a[p]
是char*
,因此*a[p]
是char
。这会导致警告 - 您正在尝试将char
转换为char*
,这绝对令人担忧。要解决此问题,请尝试删除星标。
你的代码中存在类似的错误,你将无关的星星放入其中,这意味着你在char
而不是char*
传递。这解释了你得到的几乎所有警告。尝试修复此问题,看看是否能解决警告问题。
希望这有帮助!