How to save value of a function that returns a char pointer in a string array

Asked

Viewed 509 times

0

I would like to know how I can store the return of the function below, which generates a random string, in a vector of strings with 100 of these words generated by the function, to then make a sort - increasing and decreasing - in the vector.

char* geraStringAleatoria(){

char *validchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *novastr;
register int i;
int str_len;

// novo tamanho
str_len = (rand() % MAX_STR_SIZE );

// checa tamanho
str_len += ( str_len < MIN_STR_SIZE ) ? MIN_STR_SIZE : 0;

// aloca memoria
novastr = ( char * ) malloc ( (str_len + 1) * sizeof(char));
if ( !novastr ){
    printf("[*] Erro ao alocar memoria.\n" );
    exit ( EXIT_FAILURE );
}

// gera string aleatória
for ( i = 0; i < str_len; i++ ) {
    novastr[i] = validchars[ rand() % strlen(validchars) ];
    novastr[i + 1] = 0x0;
}

   return novastr;
}

In the main function I can assign the return of the function to a string:

int main() {
    char *str;
    str = geraStringAleatoria();

   return 0;
}

But I don’t know how to store each generated word in a string array, as if each vector position were a word.

  • 2

    what have you tried? what problems are you facing?

  • I tried to store the function return in a string array: char words[ i ][ j ] = generatStringAleatoria(); - i being the number of characters and j the number of words. But I think this doesn’t make much sense, I don’t know how to do I’m beginner, I believe I’m assigning different types, and I don’t know how to store the function return in a vector with the generated words

  • Another thing: Identation is not a toy. Use, and use it in a way that makes it easier to read your program. If you are too lazy, there are several tools to do this automatically, both in the editors used for programming and online. The only purpose of identation in a C-porgram is to make reading the program easier. If you use inconsistent identation, it gets harder rather than easier.

  • Gold detail, in its function of generating strings, the line novastr[i + 1] = 0x0; can stay out of the for. There is no problem the string being inconsistent while being generated (unless it was a multi-thread program, and there was another code trying to read the same data structure in parallel)

1 answer

2

So - what you need to have there in your main function is a pointer vector for strings: each position in the array will actually contain the address of a string.

Thus, each position in its vector points to a string in a different position from memory. Later, your(s) sort(s) function will only need to exchange the values that are in that place structure - the strings will continue where they are created.

You can both have a fixed maximum size vector, declared in the program:

main () {
   char * dados[100]; 
} 

how can you use to simply declare that it is a pointer to pointers, and allocate memory dynamically:

main () {
  char ** dados;
  int tamanho;
  tamanho = ... // obtem o tamanho de alguma fonte externa
  dados = malloc(tamanho * sizeof(char *));

}

In both cases, just make a loop by calling its generative functionStringAleatoria, and each call put the returned pointer in a different position in that structure.

main () {
  ...
  for (i = 0; i < tamanho; i++) {
    dados[i] = geraStringAleatoria();
  }
  ...
}

It is always necessary to keep in mind that C does not have "strings" - they act almost as if they were a string, but they are all actually a sequence of bytes in a memory address - and whenever we pass or return a string as a parameter, we are actually passing and recovering this address (pointer)

Browser other questions tagged

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