gpt4 book ai didi

c - 对指针数组进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 08:38:40 25 4
gpt4 key购买 nike

我正在尝试对字符串数组执行插入排序。该数组被格式化为指向 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

任何关于我哪里出错的想法都会很棒。

最佳答案

看看这段代码:

key = *a[p];

这里,key 是一个char*a 是一个char**。这意味着 a[p] 是一个 char*,所以 *a[p] 是一个 char。这会导致警告 - 您正在尝试将 char 转换为 char*,这绝对令人担忧。要解决此问题,请尝试移除此处的星号。

您在整个代码中都存在类似的错误,您在其中放置了无关的星号,这意味着您传递的是 char 而不是 char*。这几乎解释了您收到的所有警告。尝试解决此问题,看看是否能解决警告。

希望这对您有所帮助!

关于c - 对指针数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com