String reading

Asked

Viewed 67 times

1

char nome[100], endereco[100], sexo[100];

   printf("Digite seu nome: ");
   scanf("%[^\n]s", nome); 

   printf("Digite seu endereço: ");
   scanf("%[^\n]s", endereco);

   printf("Qual seu sexo: ");
   scanf("%[^\n]s", sexo);

This having error in reading Strings address and sex, when I read the name in the name entry (EX: Lucas Martins), rotates normally, but when it arrives to read address then does not allow to put the address, give error ! For what reason the error ?

  • try to use the word address without the.

  • If you want to answer your own question, it may be useful for others who have the same doubt.

2 answers

2

I got.

I did so

char nome[100], endereco[100], sexo[100];

printf("Digite seu nome: "); 
scanf(" %[^\n]", nome);

printf("Digite seu endereço: "); 
scanf(" %[^\n]", endereco);

printf("Qual seu sexo: "); 
scanf(" %[^\n]", sexo);

I took out the S because the brackets already make explicit that it is the reading of a string, and I only moved the % of the first " so that it was allowed to read the next entry!

0

I know that the function scanf works with pointers, and one of its arguments would be to pass the reference'&', that is the memory address where it is separated for its program variables.
Ex.: char name, scanf('Type something:',&name);I believe when working with vectors you will have to do the 'index increment', type a[0],a[1],a[2]...,
If you want to not worry about this you will have to work with pointers, because all the 'address' manipulation the compiler has already grafted into the code when using pointers, example:
#define buffer 100; char name[buffer]; char *str; str=name; and finally scanf("text for string vector",str);
You know why all this:: Because the pointer is a 'pointer', a pointer to a memory address of the variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.