String reading

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);

I have an error reading the Strings address and sex, when I read the name in the name entry (EX: Lucas Martins), it runs normally, but when it arrives to read address then it does not allow to put the address, give error! Why the error?

    
asked by anonymous 15.08.2017 / 02:06

2 answers

3

I did it.

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 the S because the brackets already make it explicit that it is the reading of a string, and I only removed the% of the first "causing the next entry to be read!     

15.08.2017 / 02:23
0

I know that the scanf function works with pointers, and one of its arguments would be to pass the '&' reference, ie the memory address where it is separated for its program variables. : 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 manipulation of 'addresses' the compiler already grafted in the code when using pointers, eg: #define buffer 100; char name [buffer]; char * str; str = name; and finally scanf ("text to vector string", str);
You know why all this ::: Because the pointer is a 'pointer', a pointer to a memory address of the variable.

    
15.08.2017 / 02:30