gpt4 book ai didi

c - 无法读取字符串的字符

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

有两个函数,第一个计算单词的权重,第二个是检查哪个单词的值最大并返回它的地址,我的问题是每当我制作双指针并向其中插入值时“插入”指向我的函数的指针它无法读取其中的值。(不是最好的英语对不起我尽力了)

只有在主函数中复制我的函数并循环运行它时它才有效,我还是新手所以试着解释这个问题,如果是愚蠢的问题请不要生气!

#include<stdio.h>
#include<string.h>

int calc_weight(char* word);
char* max_weight(char* s[], int n);

int main()
{
char s[5][10];
for (int i = 0; i < 5; i++)
scanf("%s", (s + i));
printf("%s\n",max_weight(s, 5));


return 0;
}
int calc_weight(char* word) //calculates weight of the word
{ //assuming this function gets only small letters a...z
int counter = 0;
for (int i = 0; i < strlen(word); i++)// a=1,b=2...
counter += word[i] - 'a' + 1;
return counter;
}
char* max_weight(char* s[], int n)
{
int maxind = 0;
for (int i = 1; i < n; i++)
if (calc_weight(s + i) > calc_weight(s + maxind))
maxind = i;
return (s+maxind);
}

最佳答案

max_weight 中,当您编写 char* s[] 时,它意味着:

declare s as array of pointer to char

但您没有传递指针数组。

而是像 char s[][10] 那样做:

char* max_weight(char s[][10], int n)

告诉 s 是一个指向 10 个字符的数组的指针。

关于c - 无法读取字符串的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54490946/

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