(*words)[n] = malloc((strlen(word) + 1) * sizeof(char)); 不能将void *类型的值分配到char *类型的实体
时间: 2023-11-18 07:04:58 浏览: 26
这段代码中,使用了void指针来分配内存,并且没有进行类型转换,导致编译器无法确定分配的内存空间的数据类型,因此会报错。正确的做法是将void指针转换为char指针类型,如下所示:
```c
(*words)[n] = (char*)malloc((strlen(word) + 1) * sizeof(char));
```
这样就可以将void类型的值分配到char类型的实体中了。需要注意的是,虽然这样做可以解决编译错误,但是在实际使用时,我们应该尽量避免使用void指针,因为它会导致类型不确定性,增加程序出错的风险。
相关问题
#include<stdio.h> #include<stdlib.h> #include<string.h> int i=0; char *Getword(char *str); int main() { char *pword, str[100], a[50][10]={0},tempt[10]; int num[50]={0},k=0,j,flag=0,t; gets(str); pword = Getword(str); strcpy(a[k],pword); num[k++]++; while(1) { pword = Getword(str); for(j=0;j<k;j++) if(strcmp(a[j],pword)==0) { num[j]++; flag = 1; break; } if(flag==0) { strcpy(a[k],pword); num[k++]++; } if(str[i-1]!='\0') break; } for(i=1;i<k;i++) for(j=0;j<k-i;j++) { if(strcmp(a[j],a[j+1])>0) { strcpy(tempt,a[j]); strcpy(a[j],a[j+1]); strcpy(a[j+1],tempt); t = num[j]; num[j] = num[j+1]; num[j+1] = t; } } for(i=0;i<k;i++) printf("%s %d\n",a[i],num[i]); return 0; } char *Getword(char *str) { static char s[10]; int j=0; for(;str[i]!='\0'&&str[i]!=' ';i++) s[j++] = str[i]; s[j] = '\0'; i++; return(s); }我的代码统计单词数出现了一些问题,请问该怎样修改
在你的代码中,统计单词数的方式是通过将输入的字符串分割成单个单词,然后对每个单词进行计数。然而,你的 `Getword` 函数可能会出现问题,因为它只能处理长度为不超过 10 个字符的单词,而且还没有检查越界情况。此外,你的代码中还存在一些其他的问题,如:使用 `gets` 函数存在缓冲区溢出的风险,使用 `strcpy` 函数时应该先检查目标数组的长度等等。
为了修复这些问题,你可以使用更安全的输入函数 `fgets`,并使用字符串处理函数 `strtok` 来分割单词。同时,你可以将单词存储在动态分配的内存中,以避免长度限制和越界问题。具体的修改方法如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORDS 50
#define MAX_WORD_LEN 100
char **split_words(char *str, int *num_words);
void free_words(char **words, int num_words);
int cmp_word(const void *a, const void *b);
int main() {
char str[MAX_WORD_LEN];
char **words;
int num_words, i;
fgets(str, MAX_WORD_LEN, stdin); // 使用 fgets 输入字符串,避免缓冲区溢出
words = split_words(str, &num_words); // 使用 split_words 函数分割单词
qsort(words, num_words, sizeof(char *), cmp_word); // 使用 qsort 函数对单词进行排序
for (i = 0; i < num_words; i++) {
printf("%s\n", words[i]);
}
free_words(words, num_words); // 释放动态分配的内存
return 0;
}
// 分割字符串为单词,并返回单词数组和单词数量
char **split_words(char *str, int *num_words) {
char *word;
char **words = malloc(MAX_WORDS * sizeof(char *));
int count = 0;
word = strtok(str, " \n"); // 使用 strtok 函数分割单词
while (word != NULL) {
words[count] = malloc((strlen(word) + 1) * sizeof(char)); // 为每个单词分配动态内存
strcpy(words[count], word);
count++;
word = strtok(NULL, " \n");
}
*num_words = count;
return words;
}
// 释放单词数组中每个单词的动态内存,以及单词数组本身的动态内存
void free_words(char **words, int num_words) {
int i;
for (i = 0; i < num_words; i++) {
free(words[i]);
}
free(words);
}
// 比较函数,用于 qsort 函数排序
int cmp_word(const void *a, const void *b) {
return strcmp(*(const char **) a, *(const char **) b);
}
```
这样,你就可以更安全地统计单词数了。
使用c语言编写程序:输入5个英文单词,保存到字符串数组中,按照字母顺序从小到大输出。 要求:编写函数 void strsort (char *s[], int n),对字符指针数组 s 中的 5个单词排序。
### 回答1:
以下是使用C语言编写的程序,实现输入5个英文单词,保存到字符串数组中,按照字母顺序从小到大输出的功能:
```c
#include <stdio.h>
#include <string.h>
void strsort(char *s[], int n);
int main() {
char *words[5];
int i;
printf("请输入5个英文单词:\n");
for (i = 0; i < 5; i++) {
char word[20];
scanf("%s", word);
words[i] = malloc(strlen(word) + 1);
strcpy(words[i], word);
}
strsort(words, 5);
printf("按照字母顺序从小到大输出:\n");
for (i = 0; i < 5; i++) {
printf("%s\n", words[i]);
}
return 0;
}
void strsort(char *s[], int n) {
int i, j;
char *temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (strcmp(s[i], s[j]) > 0) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
```
在上面的程序中,我们首先定义了一个字符指针数组 `words`,用来保存输入的5个英文单词。然后我们循环5次,每次输入一个单词,将其保存到字符串数组中。
接着,我们调用了一个名为 `strsort` 的函数,对字符指针数组 `words` 中的5个单词进行排序。排序使用了简单的选择排序算法。
最后,我们按照字母顺序从小到大输出排序后的单词。注意,我们在使用 `malloc` 分配内存的时候,需要将字符串的长度加1,用来存储字符串结尾的空字符。
### 回答2:
使用C语言编写程序,实现输入5个英文单词,并按照字母顺序从小到大输出的要求。首先,我们需要编写一个函数`strsort (char *s[], int n)`来对字符指针数组`s`中的5个单词进行排序。
函数的实现思路如下:
1. 定义一个临时指针`t`和一个整数变量`i`,用于临时存储单词和作为循环变量。
2. 使用冒泡排序法对单词进行排序,外层循环从0到`n-1`,内层循环从`i+1`到`n`,每次比较相邻两个单词,如果前一个单词大于后一个单词,则交换它们的位置。
3. 在交换位置的过程中,使用`strcpy`函数将单词内容复制到临时指针`t`中进行保存,然后再将后一个单词的内容复制到前一个单词的位置,最后将临时指针`t`的内容复制到后一个单词的位置。
4. 循环结束后,即完成了对单词的排序。
下面是代码实现示例:
```c
#include<stdio.h>
#include<string.h>
void strsort(char *s[], int n) {
int i, j;
char *t;
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (strcmp(s[i], s[j]) > 0) {
t = s[i];
s[i] = s[j];
s[j] = t;
}
}
}
}
int main() {
int i;
char *words[5];
printf("请输入5个英文单词:\n");
for (i = 0; i < 5; i++) {
char word[50];
scanf("%s", word);
words[i] = malloc((strlen(word) + 1) * sizeof(char));
strcpy(words[i], word);
}
strsort(words, 5);
printf("按照字母顺序排序后的单词为:\n");
for (i = 0; i < 5; i++) {
printf("%s\n", words[i]);
}
for (i = 0; i < 5; i++) {
free(words[i]);
}
return 0;
}
```
在主函数中,我们首先定义了一个字符指针数组`words`,用于保存5个单词。然后通过循环,获取用户输入的5个英文单词,并使用`malloc`函数为每个单词动态分配内存空间。最后,调用`strsort`函数对单词进行排序,并输出排序后的结果。
需要注意的是,在程序运行结束后,要记得使用`free`函数释放动态分配的内存空间,避免内存泄漏问题。
### 回答3:
首先,我们定义一个字符指针数组`char *s[]`来保存输入的5个英文单词。接下来编写排序函数`void strsort(char *s[], int n)`,其中`n`表示要排序的单词个数。
在排序函数中,我们采用冒泡排序算法对单词进行排序。具体步骤如下:
1. 使用两层循环,外层循环进行n-1次迭代,内层循环进行一次完整的冒泡排序过程。
2. 在内层循环中,比较相邻的两个单词的字母顺序。如果前一个单词的字母顺序大于后一个单词的字母顺序,就进行交换。
3. 冒泡排序过程中,每一次迭代都会将最大的单词放在最后的位置,所以每次迭代后,可以将内层循环的比较范围减少一个。
4. 经过n-1次迭代后,字符指针数组`s`中的5个英文单词将按照字母顺序从小到大排列。
具体代码如下:
```c
#include <stdio.h>
#include <string.h>
void strsort(char *s[], int n) {
int i, j;
char *temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (strcmp(s[j], s[j + 1]) > 0) {
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
int main() {
int i;
char *words[5];
printf("请输入5个英文单词:\n");
for (i = 0; i < 5; i++) {
words[i] = (char *)malloc(100 * sizeof(char));
scanf("%s", words[i]);
}
strsort(words, 5);
printf("按照字母顺序从小到大输出的单词为:\n");
for (i = 0; i < 5; i++) {
printf("%s\n", words[i]);
}
return 0;
}
```
这样,代码就可以实现输入5个英文单词并按照字母顺序从小到大输出的功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)