Something simpler than that to allocate a dynamically typed text?

Asked

Viewed 71 times

1

// FUNCAO //
char *nome(char text[20])
{
   char *n;
   int n2;
   int n3 = 0;
   printf("%s\n",text);
   while((n2 = getchar()) != '\n' && n2 != EOF)
   {      
       if(n3 < 1)
       {
            n = (char*) malloc(sizeof(char));// alocando 1 bytes
            *n = n2;// posicao 0 e = letra digitada
            n3++; // encrementando 1 pra sair da condicao if
       }
       // Daqui pra frente só realoca //
       n3++; 
       n = (char*) realloc(n, n3*sizeof(char));
       *(n+(n3-1)) = n2;
    }
    return n;// retorna o ponteiro
 }
 //Usando a funcao //
 char *name = nome("Nome:");// texto antes de digitar o nome 
 for(int i = 0; i < 5; i++) // ignora i < 5 ainda vo mexer aqui 
       printf("%c", *(name+i)); // mostra o nome    
 free(name);

1 answer

1


There are small improvements that can be made in the code, mostly only cosmetic, stylistic and things like that, even to favor the legibility that is bad.

Could put a null character at the end to become a string and then you wouldn’t need the ribbon to print the string. What alias is wrong, the only solution to solve the size to be printed is the null character or return the size in some way as well. In fact 5 is wrong.

Note that it is unlikely that you want to leave the entry unlimited. And that there is usually nothing wrong with allocating the maximum value that a string can be input. Even if the maximum is a little too big can still optimize allocations.

It is common to start with a minimum size, type 16, 64, 256 or even more. And double the allocation with the realloc() whenever capacity burst. You have to control the occupied size and confront with the full capacity to decide whether to relocate.

Besides sizeof(char) is always 1, so this is unnecessary, the cast (char*) is inappropriate in C.

You do not need to make the first allocation as an exception on if, the realloc() just from the beginning pointer as null.

  • it worked out to start the pointer as null and then just relocate, but I didn’t understand it very well, it has how to explain this vlw business better

  • There’s a better way to ask this business?

  • why start as null ? has an explanation

  • Because there the relocation occurs from nothing (the null one) to somewhere. So the effect is the same as the malloc(), it allocates something new, but since it has nothing to dislocate, everything works as expected, thus eliminating the exception in the algorithm.

  • vlw help, and pro loop I used: for (int i = 0; i < strlen(name); i++){ show}

  • https://answall.com/q/167528/101 I wouldn’t do that. I wouldn’t use a loop.

  • to having a problem with while when I use the free() it gives an error, already without the free it goes normal, very strange

  • This seems to be another problem, ask a question with details so we can help.

Show 3 more comments

Browser other questions tagged

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