How to receive a keyboard pointer in C / C ++?

2

How do I get a pointer to char via keyboard?

    
asked by anonymous 17.10.2014 / 18:32

2 answers

4

Basically in the same way as other data types. I know you've already learned in another question how to use scanf() . There are two differences to read an integer you already know.

scanf("%s", texto);

The first argument indicates that you are trying to read a string . The second is to pass the variable that will receive this data. Note that you do not use the & operator in this case because the variable is already a pointer.

#include <stdio.h>
#include <stdlib.h>
int main(){
   char *texto; //declara a variável da forma como você sugeriu
   texto = malloc(31); //reserva o espaço em memória para 30 caracteres
   scanf("%s30", texto); //Lê caracteres pelo teclado e guarda os primeiros 30 em texto
   printf("%s", texto); //imprime o que foi entrado.
   free(texto); //libera a memória alocada
   return 0;
}

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

You can also use array notation that Neither C nor more is another way of working with pointers. This makes it easier to allocate memory. In this case the allocation will usually occur in stack and not in the heap as is done with malloc . For simple examples it is easier. See Guilherme Bernal's answer for an example with array . I used the pointer because that was the term you used.

It is possible to allocate memory for an object pointed to by a variable as done above in stack also, as done with array . This practice is not much recommended. The code below is equal to malloc but instead of allocating dynamic memory (in heap ), it allocates memory that is considered static (which does not need to be released later, read more to understand this in the link that I went over):

char * texto = alloca(31);

Never allocate too large a memory in stack . Do not use this area of memory to return the value from one function to another, directly or indirectly. If you need to do this you would have to copy the data in return since closing a function would potentially cause the data allocated in the stack to be lost.

I will not go into detail as it seems like you are starting. When you have specific questions about this, you can open another question if it does not already exist on the site.

In any case, it is common to use pointer to dynamic allocations in heap and use array for allocation when you know it's best to use stack . You do not have to invent much.

    
17.10.2014 / 18:41
2

You can read a string directly from the console's default entry using the scanf command. So:

#include <stdio.h>

int main() {
    printf("Digite uma frase: ");
    char frase[300];
    scanf("%s", frase);
    pritnf("Voce digitou '%s'", frase);
    return 0;
}

Note, however, that there is a risk of the user typing more than 299 characters. (plus the null terminator that the entire string has, totaling 300). If it does, there will be "buffer overflow" and your program is likely to crash. To avoid it you can use it like this:

scanf("%299s", frase);

Any number of letters after the first 299 will not be read.

    
17.10.2014 / 18:41