Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple c error makes pointer from integer without a cast

Tags:

c

pointers

I've undertaken learning c with the aid of the k & r book. Pretty exciting but I've run into trouble early on and I'm not sure how to fix the problem.

I'm trying out some really simple sample code and I'm getting the following error. I don't understand why because the code is straight out of the book.

main.c:11: warning: passing argument 2 of ‘sprintf’ makes pointer from integer without a cast


#include <stdio.h>

/* copy input to output; 1st version */
main() {
    int i;
    int power(int base, int n);

    for (i = 0; i < 10; i++) {
        sprintf("%d %d %d\n", i ,power(2, i), power(-3, i));
        return 0;
    }



}

int power(int base, int n) {
    int i;
    int p;

    p = 1;

    for (i = 1; i <= n; ++i)
        p = p * base;
    return p;

}

I'd appreciate a nudge to get me going on my way again.

like image 892
dubbeat Avatar asked Nov 22 '10 20:11

dubbeat


2 Answers

sprintf is for creating a string based on some formatting. It looks like you want output, so you want to use printf.

Also, return 0; should not be be enclosed in your for loop. It would result in termination of the program after one iteration.

like image 89
逆さま Avatar answered Nov 05 '22 06:11

逆さま


From man sprintf: int sprintf(char *str, const char *format, ...);

The first argument to sprintf is the string you have allocated.

If you want to print to the standard output (usually the terminal in which you run the program), use printf instead.

like image 3
rmk Avatar answered Nov 05 '22 06:11

rmk