0
Oops, I wonder how the & (and commercial) works, in the case of the printf, in the scanf I know, that it takes the variable to a memory address, but in the case of the printf? I have this program here, and I don’t know very well the function of this &, I just know that without it, the program doesn’t run rs
char nome[30], sobrenome[20], ch;
printf("Informe seu nome: ");
scanf("%29[^\n]", nome);
int i;
for (i = 0; nome[i] != ' ' && nome[i] != '\0'; i++) {
printf("%c", nome[i]); // Para mostrar o primeiro nome
}
printf("\nBoa noite, %s!\n", &nome[i+1]); // => nome == &nome[0]
system("Pause");
return 0;
In this printf you are specifying the %s format code, that is, you want to print a string. The string is identified by the address of the initial character and the end of the string by the character ' 0'. Hence it is specified &name[i+1], that is the address of the name position i+1. As in the previous loop the condition of permanence is name[i] != ' ' && name[i] != ' 0' will be printed something that makes sense only if name[i] == ' '.
– anonimo
I think I understand what you mean, I’m gonna run some tests here, thanks!!!
– Brenno Carvalho
An outline situation for you to test: if the name entered in your entry does not have a blank space your program will print memory junk.
– anonimo
yeah, I just did a test, and it actually printed out memory junk, I get it!!!
– Brenno Carvalho