错误:char之前的预期表达式

时间:2014-04-12 13:34:45

标签: c pointers compiler-errors syntax-error

我是学习C的新手,我很难搞清楚语法。我一直在编译器中的char之前得到[error]期望的表达式,我想知道是否有人可以向我解释它。这是我的代码:

#include <stdio.h>

void convert_weight(int x, char a[], int* y, char b[])
{
int i;
for(i=0; char a[i] != '\0';i++)

    if(char a[i] == 'l') {

        *y/2.2;
    }   
    else {
        if(char a[i] == 'k')
        {

        *y *2.2;
    }
    }


}

int main() {
  char newline, another = 'y';
  int weight1, weight2;
char units1[4], units2[4]; // length 4 because of '\0'
while (another == 'y') {
    printf("Enter a weight and the units of the weight (lbs or kgs)\n");
    scanf("%d %s", &weight1, units1);
    convert_weight(weight1, units1, &weight2, units2);
    printf("%d %s = %d %s\nAnother (y or n)\n", weight1, units1, weight2, units2); 
    scanf("%c%c", &newline, &another)    ;
  }
  return 0;
}

1 个答案:

答案 0 :(得分:2)

您不需要重新声明in:

for(i=0; char a[i] != '\0';i++)

删除这些行上的char

void convert_weight(int x, char a[], int* y, char b[])
{
   int i;
   for(i=0; a[i] != '\0';i++)
   {
       if(a[i] == 'l') {
          *y /= 2.2;
       }   
       else if(a[i] == 'k') {
          *y *= 2.2;
       }
   }
}