C语言中使用指针的转换

C语言中使用指针的转换,c,C,我要用警告来编译它。它正确地进行转换,但单位实际读数为“EoL” 我得到了一个缺失的“(“语法错误。我已经仔细检查了很久,似乎找不到它 此外,我还收到以下错误: hello.c:22:警告:convert\u-weight的类型冲突 我在各自的函数中使用int、char和指针,因此不理解冲突 有什么想法吗 #include <stdio.h> #include <string.h> int main() { char newline, another = '

我要用警告来编译它。它正确地进行转换,但单位实际读数为“EoL”

我得到了一个缺失的“(“语法错误。我已经仔细检查了很久,似乎找不到它

此外,我还收到以下错误: hello.c:22:警告:convert\u-weight的类型冲突

我在各自的函数中使用int、char和指针,因此不理解冲突

有什么想法吗

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




int main() {

  char newline, another = 'y';
  int weight1, weight2;
  char units1[4], units2[4];
  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;
}
void convert_weight(int weight1, char  units1, int* weight2 , char  units2)
{
  char lbs[4]= "lbs";
  char kgs[4]= "kgs";
  int diffkgs =  strcmp(units1,kgs);
  int difflbs =  strcmp(units1,lbs);

  if (diffkgs==0){

    &weight2 = weight1 * 2.2;
    units2 = "lbs";
  }
  else if (difflbs==0){
    &weight2 = weight1 * .4545;
    units2 = "kgs";
  }

}
#包括
#包括
int main(){
char换行符,另一个='y';
int权重1,权重2;
字符单位1[4],单位2[4];
而(另一个=='y'){
printf(“输入重量和重量单位(磅或千克)\n”);
scanf(“%d%s”,权重1,单位1);
转换单位重量(重量1,单位1和重量2,单位2);
printf(“%d%s=%d%s\n其他(y或n)”,权重1,单位1,权重2,单位2);
scanf(“%c%c”、&newline和另一个);
}
返回0;
}
void convert_weight(整数权重1,字符单位1,整数*权重2,字符单位2)
{
字符磅[4]=“磅”;
字符kgs[4]=“kgs”;
int diffkgs=strcmp(单位1,千克);
int difflbs=strcmp(单位1,磅);
如果(diffkgs==0){
&权重2=权重1*2.2;
units2=“磅”;
}
否则如果(difflbs==0){
&权重2=权重1*.4545;
units2=“kgs”;
}
}
我做了一些更改,现在我只收到以下语法错误消息:

hello.c:23:error:expected-before-units1-before-before-units1-

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




int main() {

  char newline, another = 'y';
  int weight1, weight2;
  char units1[4], units2[4];
  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;
}


void convert_weight(int weight1, char[] units1, int* weight2 , char[]  units2)
{
  char lbs[4]= "lbs";
  char kgs[4]= "kgs";
  int diffkgs =  strcmp(units1,kgs);
  int difflbs =  strcmp(units1,lbs);

  if (diffkgs==0){

    *weight2 = weight1 * 2.2;
    units2 = "lbs";
  }
  else if (difflbs==0){
    *weight2 = weight1 * .4545;
    units2 = "kgs";
  }
#包括
#包括
int main(){
char换行符,另一个='y';
int权重1,权重2;
字符单位1[4],单位2[4];
而(另一个=='y'){
printf(“输入重量和重量单位(磅或千克)\n”);
scanf(“%d%s”,权重1,单位1);
转换单位重量(重量1,单位1和重量2,单位2);
printf(“%d%s=%d%s\n其他(y或n)”,权重1,单位1,权重2,单位2);
scanf(“%c%c”、&newline和另一个);
}
返回0;
}
void convert_weight(整数权重1,字符[]单位1,整数*权重2,字符[]单位2)
{
字符磅[4]=“磅”;
字符kgs[4]=“kgs”;
int diffkgs=strcmp(单位1,千克);
int difflbs=strcmp(单位1,磅);
如果(diffkgs==0){
*权重2=权重1*2.2;
units2=“磅”;
}
否则如果(difflbs==0){
*权重2=权重1*.4545;
units2=“kgs”;
}

您已经这样定义了
convert\u weight()

void convert_weight(int weight1, char  units1, int* weight2 , char  units2)
convert_weight(weight1, units1,&weight2, units2);
但你这样称呼它:

void convert_weight(int weight1, char  units1, int* weight2 , char  units2)
convert_weight(weight1, units1,&weight2, units2);

传递字符串,而不是定义的字符。

< P>以下版本工作,使用Visual C++ 2013的C编译器进行测试。

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

void convert_weight(int weight1, char  *units1, int* weight2 , char  units2)
{
  char lbs[4]= "lbs";
  char kgs[4]= "kgs";
  int diffkgs =  strcmp(units1,"kgs");
  int difflbs =  strcmp(units1,"lbs");

  if (diffkgs==0){

    *weight2 = weight1 * 2.2;
     units2 = "lbs";
  }
  else if (difflbs==0){
    *weight2 = weight1 * .4545;
    units2 = "kgs";
  }

}

int main() {

  char newline, another = 'y';
  int weight1, weight2;
  char units1[4], units2[4];
  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;
}
#包括
#包括
void convert_weight(整数权重1,字符*单位1,整数*权重2,字符单位2)
{
字符磅[4]=“磅”;
字符kgs[4]=“kgs”;
int diffkgs=strcmp(单位1,“千克”);
int difflbs=strcmp(单位1,“磅”);
如果(diffkgs==0){
*权重2=权重1*2.2;
units2=“磅”;
}
否则如果(difflbs==0){
*权重2=权重1*.4545;
units2=“kgs”;
}
}
int main(){
char换行符,另一个='y';
int权重1,权重2;
字符单位1[4],单位2[4];
而(另一个=='y'){
printf(“输入重量和重量单位(磅或千克)\n”);
scanf(“%d%s”,权重1,单位1);
转换单位重量(重量1,单位1和重量2,单位2);
printf(“%d%s=%d%s\n其他(y或n)”,权重1,单位1,权重2,单位2);
scanf(“%c%c”、&newline和另一个);
}
返回0;
}
你的问题太多了

  • 要么在main上方编写函数定义,要么在main上方添加函数原型 像这样,

    void convert_weight(整数权重1、字符单位1、整数权重2、字符单位2)

  • strcmp(units1,“kgs”);采用指向字符的指针,而不是字符,其第二个值是字符串,因此必须用双引号括起来。“lbs”行也是如此

  • 正如注释和其他答案所暗示的那样,您可能希望使用*weight2=weight1*.4545;而不是&*weight2=weight1*.4545;运算符返回指针变量本身的地址,但*可用于在指针指向的地址处存储/访问值(这里的权重为2)


  • 您发布的代码没有显示丢失的“(”错误。但是它还有许多其他错误。您应该逐字发布生成日志,而不是对错误进行解释或描述。您的原始代码在以下位置生成以下内容:

    下面的内容在结尾行注释所指示的地方进行了更改,尽管有很多不明智的编码,但这是一个不同的问题

    #include <stdio.h>
    #include <string.h>
    
    void convert_weight(int weight1, char* units1, int* weight2 , char* units2) ; // ADDED
    
    int main() {
    
      char newline, another = 'y';
      int weight1, weight2;
      char units1[4], units2[4];
      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;
    }
    
    void convert_weight(int weight1, char* units1, int* weight2 , char* units2) // CHANGED
    {
      char lbs[4]= "lbs";
      char kgs[4]= "kgs";
      int diffkgs =  strcmp(units1,kgs);
      int difflbs =  strcmp(units1,lbs);
    
      if (diffkgs==0){
    
        *weight2 = weight1 * 2.2; // CHANGED
        strcpy( units2, "lbs" ) ; // CHANGED
      }
      else if (difflbs==0){
        *weight2 = weight1 * .4545; // CHANGED
        strcpy( units2, "lbs" ) ; // CHANGED
      }
    
    }
    
    #包括
    #包括
    void convert_weight(int weight1,char*units1,int*weight2,char*units2);//添加
    int main(){
    char换行符,另一个='y';
    int权重1,权重2;
    字符单位1[4],单位2[4];
    而(另一个=='y'){
    printf(“输入重量和重量单位(磅或千克)\n”);
    scanf(“%d%s”,权重1,单位1);
    转换单位重量(重量1,单位1和重量2,单位2);
    printf(“%d%s=%d%s\n其他(y或n)”,权重1,单位1,权重2,单位2);
    scanf(“%c%c”、&newline和另一个);
    }
    返回0;
    }
    void convert_weight(int weight1,char*units1,int*weight2,char*units2)//已更改
    {
    字符磅[4]=“磅”;
    字符kgs[4]=“kgs”;
    int diffkgs=strcmp(单位1,千克);
    int difflbs=strcmp(单位1,磅);
    如果(diffkgs==0){
    *weight2=weight1*2.2;//已更改
    strcpy(单位2,“磅”);//已更改
    }
    否则如果(difflbs==0){
    *weight2=weight1*.4545;//已更改
    strcpy(单位2,“磅”);//已更改
    }
    }
    
    您的参数
    units2
    的类型为
    char
    ,它只包含一个字符。在函数末尾有
    units2=“kgs”
    。这不起作用。你需要修复它的类型。
    &weight2=weight1*2.2;
    --这当然不是你想要的。丢失
    &
    ,用
    *
    替换它,很可能是。
    units2=“lbs”
    strcpy(units2,“lbs”);
    ,需要在ma之前使用原型