C 错误赋值从整数生成指针而不使用强制转换[默认情况下启用]

C 错误赋值从整数生成指针而不使用强制转换[默认情况下启用],c,C,我对C编程相当陌生。我在编译程序时遇到了这个错误。我曾试图在互联网上寻找解决方案,但没有成功。错误出现在num=rand()%20 char* getParameter(char *name) { char *num; char *buffer; if(strcmp(name,"Trainingsprogramm")){ srand (time(NULL)); num = rand()%20;; sprintf(buffer,"%d",num)

我对C编程相当陌生。我在编译程序时遇到了这个错误。我曾试图在互联网上寻找解决方案,但没有成功。错误出现在
num=rand()%20

char* getParameter(char *name)
 {
   char *num;

   char *buffer;


   if(strcmp(name,"Trainingsprogramm")){
    srand (time(NULL));
     num = rand()%20;;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"Personnenummer")){
     srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"Tretzustand")){
     srand (time(NULL));
     srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"Tretleistung")){
     srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"Drehzahl")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"Geschwindigkeit")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"GefahreneDistanz")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"RealeKJoule")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"AktuellerPuls")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"MomentanerGang")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"RelaxBetrieb")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"VerbrauchteKJoule")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}

return buffer;

}

尝试从
num
的声明中删除星号

char *num; // <<< This is a pointer to char declaration
char num; // <<< This is a char

int num; // <<< But this is probably what you meant/wanted!
char*num;//
赋值使指针从整数

代码尝试将的整数结果分配给字符指针
num

char *num; // <<< This is a pointer to char declaration
char num; // <<< This is a char

int num; // <<< But this is probably what you meant/wanted!
根据目前显示的代码,将
num
更改为整数,如

 int num;
将解决此问题。

rand()
返回一个
int
,对其应用
%
运算符也会返回一个
int
-您应该将此结果保存在
int
中,而不是
char*

int num;
char *buffer;

更改
char*num
int num
,为
buffer
分配内存,在
main
的开头调用
srand
一次。为什么要连续调用srand?谢谢,但我有一个字符缓冲区,这就是为什么我选择字符指针的原因……是否还有另一个函数可以像rand一样处理字符而不是整数。谢谢先看一看,然后再看我脑海中闪现的第一件事,那就是。这是一个简单的函数,你甚至可以。谢谢,但我有一个字符缓冲区,这就是为什么我选择了字符指针…还有其他函数可以像rand一样处理字符而不是整数。谢谢@user3628617:您指的是哪个缓冲区?@user3628617关于
printf()
(&Friends)的工作原理您可能希望在此处使用RTFM:我指的是用于接收数据的缓冲区…我发布的代码中不包括它。。。