C printf error

2

The error in question in a flag that should supposedly be correct, follow the code below:

#include <stdio.h>
#include <string.h>

int main() {

char var[50]=" Write Once Bug Everywere ";

int i,cont=0;

for (i = 0; i < 50; i++) {
    if(var[i]==' '){
        cont++;
    }
}

printf("Existem %d espaços em branco",&cont);   
}

When I mouse over the error in NetBeans it gives me the following hint:

  

"Incompatibility of argument type" int * "and specifier   conversion "d".

    
asked by anonymous 18.02.2017 / 01:22

2 answers

3

You are prompted to print the address of cont since you used the & operator. If you want to print the counter, please print it and not any other information. If you wanted to print a pointer you could use %p . Formatting Documentation .

#include <stdio.h>

int main() {
    char var[50] = " Write Once Run Everywere ";
    int cont = 0;
    for (int i = 0; i < 50; i++) {
        if (var[i] == ' ') {
            cont++;
        }
    }
    printf("Existem %d espaços em branco", cont);   
}

See running on ideone . And No Coding Ground . Also put it on GitHub for future reference .

I believe the mistake occurred because on scanf() , you usually require the address. This is because you will change its value, so you pass the address of the variable to the function know where to put what was typed. The printf() will only use the value, it does not need to get its address.

Read more about the operator and pointers . C is crude, you have to take care of everything.

All types can be accessed through your address. The pointer type is obviously already an address and usually the array is accessing your address.

You have to read the function documentation to see what you need to pass on to it.

    
18.02.2017 / 01:35
5

Switch

  

printf ("There are% d blanks," & cont);

by

printf("Existem %d espaços em branco",cont);

When using & , you try to print the variable address, so the error:

  

incompatibility with int *

int* is a pointer to integer, in this case, the value of the memory address that contains the cont variable.

    
18.02.2017 / 01:35