问题
Im looking for a 6 digit random number on the end of foo-, I have been trying for a few hours now with no success. only error messages
note: expected 'char *' but argument is of type 'int'
Ive tried to convert the int to char but it just doesn't like it, My code is below,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
char* concat(char *s1, char *s2)
{
char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
//in real code you would check for errors in malloc here
strcpy(result, s1);
strcat(result, s2);
return result;
}
int main ()
{
srand(time(NULL));
int r = rand();
printf(concat("foo-", r));
return 0;
}
回答1:
for example:
srand(time(NULL));
int r = rand();
char* str = (char*)malloc(sizeof(char)*20);
snprintf(str, 7, "%d", r);
if (strlen(str) < 6) { /* if r had less than 6 digits */
sprintf(str, "%06d", r);
}
char* s = concat("foo-", str);
printf("%s",s);
free(str);
free(s);
return 0;
from http://joequery.me/code/snprintf-c/ :
int snprintf(char *str, size_t size, const char *format, ...);
str
is the buffer where printf output will be redirected to.size
is the maximum number of bytes(characters) that will be written to the buffer, including the terminating null character that snprintf automatically places for you. Theformat
and the optional...
arguments are just the string formats like "%d", myint as seen in printf.
so, in order to get a 6 digit number converted, you specify in snprintf()
the size
argument to 7
(you include a null character).
sprintf()
function sends formatted output to a string pointed to by the first argument (in our case this is str
). %06d
format provides that to str
at least 6 digits will be sent.
CONCLUSION
you want to convert a 6 digit number to a char array. if the number had at least 6 digits, with snprintf()
you will get the first 6 digits of the number that was converted. if it has less than 6 digits, the sprintf()
will add zeroes at the beginning until there are 6 digits. so, if r = 101;
the result would be 000101
.
note that with your usage of concat
function:
printf(concat("foo-",r));
compiler warns about (gcc in my case):
warning: format not a string literal and no format arguments [-Wformat-security]
printf()
function needs to know the format of the string argument that is passed. there's a nice answer about it right here : warning: format not a string literal and no format arguments
回答2:
You could do this
printf("foo-%d", r);
or
char buffer[10];
sprintf(buffer, "%d", r);
char *c = concat("foo-", r);
printf("%s", c);
free(c);
This will use your function. Please read the manual pages for sprintf
and printf
回答3:
In the example I'm using sprintf
to convert int
to char[]
. Also I'm using modulo operation to ensure the result will have no more than 6 digits. The %06d
argument for sprintf
will ensure that the result will have no less than 6 digits.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char buf[7] = { 0 };
int r;
srand(time(NULL));
r = rand() % 1000000;
sprintf(buf, "%06d", r);
printf("foo-%s\n", buf);
return 0;
}
回答4:
Your problem occurs when you attempt to call concat
with the int
value r
as the second argument. Notice your definition of concat:
char* concat(char *s1, char *s2);
The second argument is char *s2
. In your program you attempt to call it with:
int r = rand();
printf(concat("foo-", r));
r
is an integer. Try this to correct the problem:
int main ()
{
srand(time(NULL));
int r = rand();
char str[] = "bar";
printf(concat("foo-", str));
return 0;
}
来源:https://stackoverflow.com/questions/24464745/concatenating-an-int-to-a-string-or-converting-in-c