A simple c error makes a pointer out of a whole without casting

I taught using books k and r. Interesting enough, but I ran into problems at an early stage, and I'm not sure how to solve the problem.

I am trying to execute a very simple code example and I am getting the following error. I donโ€™t understand why, because the code is directly from 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 would be grateful that I would go down the road again.

+3
source share
6 answers

sprintfDesigned to create strings based on some formatting. It looks like you want to output, so you want to use printf.

It return 0;should also not be enclosed in your loop for. This will end the program after one iteration.

+5

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

sprintf - , .

( , ), printf.

+3

, , :

for (i = 1; i <= n; ++i)
    p = p * base;    //inside
return p;            //outside because no brackets {}

for (i = 1; i <= n; ++i){
    p = p * base;    //inside
    return p;}       //inside because brackets {}

if, .

+1

sprintf() .

, , - .

printf() sprintf().

0

sprintf() - , . .

( snprintf() - , .)

0

, , ... , 11. " casting c" google;)

0

Source: https://habr.com/ru/post/1776305/


All Articles