gpt4 book ai didi

C - strcmp 的字节和数组中的字符串比较

转载 作者:行者123 更新时间:2023-11-30 16:53:21 27 4
gpt4 key购买 nike

提前感谢您的回答!我编写了一个程序,在我看来可能是正确的,但编译器中出现错误(关于 strcmp 的第一行):

"Wrong type: Size of element is 4 bytes, but size of String is 8 bytes."

    float total_weight_kg(Array weight_array) { // todo: modify header (parameters, parameter types, return type)
float sum = 0;
for (int i = 0; i < a_length(weight_array); i+=2)
{
if( strcmp(get(String, weight_array, (i+1) ), "kg") == 0 ){ sum = sum + get (float, weight_array, (i)) ;}
if( strcmp(get (String, weight_array, (i+1) ), "l") == 0 ){ sum = sum + get (float, weight_array, (i)) ;}
if( strcmp(get (String, weight_array, (i+1) ), "g") == 0 ){ sum = sum + ( get (float, weight_array, (i) )*1000 ) ;}
if( strcmp(get (String, weight_array, (i+1) ), "mg") == 0 ){ sum = sum + ( get (float, weight_array, (i) ) * 1000000 ) ;}
else{printf("Impossible for this case. Unknown unit of measurement.");}
}
return sum;}

最佳答案

float 为 4 字节。通过使用 get 函数来猜测,

get(String, weight_array, (i+1) )

问题在于 weight_array 是一个 float 组,但第一个参数正在使用 String 类型执行一些操作。

因此你的错误,

"Wrong type: Size of element is 4 bytes, but size of String is 8 bytes."

关于C - strcmp 的字节和数组中的字符串比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40895414/

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