How do I create a char variable with an indeterminate size

Asked

Viewed 815 times

0

How do I create a char* variable (to store a name) with an indeterminate size?

Example:

char nome[30] = { 0 } // 30 é o valor máximo e no caso se a pessoa tiver um nome grande? que ultrapasse 30?
cout << "Informe seu nome completo: " << endl;
fgets(nome, 29, stdin);
  • 1

    You want to create a string of arbitrary size. For this, you need to use pointers. I recommend putting in a class so that you can hide the implementation, with methods to increase or decrease the size of the allocated string as you need it

  • With getchar and realloc can do well, but it involves building some logic. In your case I think it best to define a wider size, such as 256 for example and just use this. Would it be awesome to read a name with 50,000 characters for example ? And how would I show it in the program ? Similarly note that databases, places where information such as the nome are stored, has limit in the same, going the varchar to the limit of 255 characters. Even a type like text, which supposedly represents unlimited text is also limited to 64KB.

  • Related https://answall.com/q/106719/101

1 answer

1


Typically, performance arrays of sufficiently large static sizes, such as 999 cells, are used. Still, in C++ has a high-level feature: flow (stream). Streams allow you to add values slowly, with no size worries (apart from architecture support). Use of class stringstream (in the library <sstream>) allows to form and then generate a structure string, this can be seen as a const char*.

In C, I think stream would have to be implemented by hand. There are several ways to do this. Another option you have is to control the lifespan of the data, including using blocks to limit the lifespan of local variables. So, you can create an awesome size array, copy it to a smaller one and delete it. So for example.

# include <stdio.h>
# include <stdlib.h>
int main( int argCount , char **argVector ){
   int tamanhoDoNome , indice ;
   char *nome ;
   {
      char nomeTemporario[999] = {0} ;
      cout << "Informe seu nome completo: " << endl ;
      fgets( nomeTemporario , 998 , stdin ) ;
      for( tamanhoDoNome=0 ; nomeTemporario[tamanhoDoNome++] ; ) ;
      nome = malloc( tamanhoDoNome*sizeof(char) ) ;
      for( indice=0 ; indice<tamanhoDoNome ; indice++ ) nome[indice]=nomeTemporario[indice] ;
   }
   // Agora não existem mais nomeTemporario e suas células, ficou nem a carcaça de sobra.
   free( nome ) ;
   // Agora não existem mais as células de nome, só existe a variável, ficou só a carcaça!
   return 0 ;
}

Any doubt?

  • Use for example 999 cells and in case the person’s name is small, and the other remaining cells will remain reserved in meso memory without using it? This wouldn’t cause any problem in the program or slow the program down a bit?

  • This reserves 999 chars each of one byte (999 bytes, nor 1Kb) and can be in local reserve, i.e., temporary in cases of called functions. If you ensure that the person’s name does not exceed a smaller number, you can use it as well. It is common to reserve 256 cells for small strings. May be even less...

Browser other questions tagged

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