1

I am quite new to C programming. I encountered this error while compiling my program. I have tried to look around the internet for a solution but to no avail. The error is on the line 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;

}
Mat
  • 202,337
  • 40
  • 393
  • 406
user3628617
  • 29
  • 1
  • 4

3 Answers3

0

Try removing the star from the declaration of 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!
nonsensickle
  • 4,438
  • 2
  • 34
  • 61
  • thanks but i have a char buffer, that is why i chose the character pointer...is there another function that works like rand for characters and not integers. Thanks!! – user3628617 May 09 '15 at 14:39
  • Have a look at [this SO answer](http://stackoverflow.com/a/228827/1667513) before looking up the first thing that popped into my head, which is [itoa](https://fresh2refresh.com/c/c-type-casting/c-itoa-function/). It's a simple function and you could even [implement it yourself](http://en.wikibooks.org/wiki/C_Programming/C_Reference/stdlib.h/itoa). – nonsensickle May 09 '15 at 23:22
0

Assignment makes pointer from integer ...

 char *num;
 ...
 num = rand()%20;

The code tries to assign the integer result of rand() to the character pointer num.

From the code you show so far, changing num to be an integer, like

 int num;

would solve this.

alk
  • 69,737
  • 10
  • 105
  • 255
  • thanks but i have a char buffer, that is why i chose the character pointer...is there another function that works like rand for characters and not integers. Thanks!! – user3628617 May 09 '15 at 14:39
  • @user3628617: Which buffer are you referring to? – alk May 09 '15 at 14:41
  • @user3628617 On how `printf()` (& Friends) works you might like to RTFM here: http://man7.org/linux/man-pages/man3/printf.3.html – alk May 09 '15 at 14:42
  • i am referring to a buffer i use to receive data...it is not included in the code i posted... – user3628617 May 09 '15 at 14:44
0

rand() returns an int, and applying the % operator on it also returns an int - you should save this result in an int, not a char*:

int num;
char *buffer;
Mureinik
  • 297,002
  • 52
  • 306
  • 350