C/passing arg 2 of 'sprintf' makes pointer from integer without a cast
Expert: Narendra - 4/22/2005
QuestionWhen trying to compile a C program including the code below, I keep getting this error and I don't know what to do to fix it. Please explain if you can!
COMPILATION ATTEMPT:
tommy@city-it ~/cdevel
$ gcc -Wall -ansi -pedantic -mno-cygwin -I . -o myfirstlib myfirstlib.c
ERROR:
In file included from myfirstlib.c:3:
myfuncs.h: In function 'getstring':
myfuncs.h:98: warning: passing arg 2 of 'sprintf' makes pointer from integer without a cast
CODE SUMMARY:
<code>
/* will act as the buffer into which user's response will be stored */
char lineofinput[255];
/* grabs a string from the command prompt */
char getstring (char strquestion[255])
{
/* reset to empty from previous calls */
char lineofinput[255];
if (strlen (strquestion))
printf ("%s", strquestion);
fgets (lineofinput, sizeof (lineofinput), stdin);
/* while we get an empty response */
while (strlen (lineofinput) == 1)
{
if (strlen (strquestion) == 1)
printf ("Empty answers not permitted. Please try again: ");
return (sprintf("%s", getstring (lineofinput)));
}
return (sprintf("%s", lineofinput));
}
</code>
Complete code may be downloaded from my website at www.atrixnet.com/aequestion.tar.gz (It's only 1 Kb)
MY COMPILER:
tommy@city-it ~/cdevel
$ gcc -v
Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/specs
Configured with: /gcc/gcc-3.3.3-3/configure --verbose --prefix=/usr --exec-prefi
x=/usr --sysconfdir=/etc --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --enable-languages=c,ada,c++,d,f77,java,objc,
pascal --enable-nls --without-included-gettext --enable-libgcj --with-system-zlib --enable-interpreter --enable-threads=posix --enable-java-gc=boehm --enable-sjlj-exceptions --disable-version-specific-runtime-libs --disable-win32-registry
Thread model: posix
gcc version 3.3.3 (cygwin special)
AnswerFor sprintf(), the first argument has to be a string variable.
For example:
char *str = (char *) malloc(100);
int i = 10;
float f = 5.34;
char mystr[] = "MY STRING";
sprintf(str, "%d %s %f", i, mystr, f);
In the above example, you can see that the first argument for sprintf() is a string variable str.
After sprintf() gets executed, the value will be stored in that.
Hope this helps.
-Narendra