C - 来自Array的strcmp和String比较的字节

时间:2016-11-30 18:15:37

标签: c strcmp

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

  

"错误的类型:元素的大小是4个字节,但字符串的大小是8   。字节"

    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;}     

1 个答案:

答案 0 :(得分:0)

浮点数是4个字节。通过使用get函数进行猜测,

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

问题是weight_array是一个浮点数组,但第一个参数是用String类型执行某些操作。

因此你的错误,

  

“错误的类型:元素的大小是4个字节,但字符串的大小是8个字节。”