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.