How to match a vector from an array with an array in the main function?

1

I need to match an array that is returning a function to an array in the main function. My goal with this is to be able to pass this same array as a parameter to other functions without having to be calling the function at the time of sending the array.

Example:

// função para retirar os espaços digitados
int retira_espaco(int tamanho, char vetor[], int t, char retorno []){ 
    int i = 0;
    int contador = 0;
    for (i=0; i<tamanho && vetor[i] != 0; i++){ //Nesse 'for' tem que se verificar se  vetor[i] !=0 pois o fgets sempre adiciona um 0 binario no final do que foi digitado
        if (isdigit(vetor[i]) || vetor[i] == 'i' || vetor[i] == 'p'
       || vetor[i] == '+' || vetor[i] == '-' || vetor[i] == '*'
       || vetor[i] == '/' || vetor[i] == '^' || vetor[i] == '='){
            retorno[contador++] = vetor[i];
        }
    }
    retorno[contador] = '
int main(){    
    printf("%s\n", retira_espaco(tamanho, vetor, tamanho, retorno));
}
'; return (retorno); }

At the time of sending the parameters to this first function I have to do the following:

// função para retirar os espaços digitados
int retira_espaco(int tamanho, char vetor[], int t, char retorno []){ 
    int i = 0;
    int contador = 0;
    for (i=0; i<tamanho && vetor[i] != 0; i++){ //Nesse 'for' tem que se verificar se  vetor[i] !=0 pois o fgets sempre adiciona um 0 binario no final do que foi digitado
        if (isdigit(vetor[i]) || vetor[i] == 'i' || vetor[i] == 'p'
       || vetor[i] == '+' || vetor[i] == '-' || vetor[i] == '*'
       || vetor[i] == '/' || vetor[i] == '^' || vetor[i] == '='){
            retorno[contador++] = vetor[i];
        }
    }
    retorno[contador] = '
int main(){    
    printf("%s\n", retira_espaco(tamanho, vetor, tamanho, retorno));
}
'; return (retorno); }

I want to match an array in the main function to the array that returns from the space_draw, how to proceed?

    
asked by anonymous 12.09.2016 / 02:11

2 answers

2

What you are trying to do is to assign the array retorno that was filled by the retira_espaco function to another array, but this is unnecessary because arrays are passed by "reference" when they are used as function parameters . That is, the array retorno within the function retira_espaco is the same array outside the function that was passed as a parameter.

Exemplifying

char x[100], y[100];

// ...outros comandos

// o array "retorno" dentro de retira_espaco na verdade e' o array y aqui de fora
retira_espaco(100, x, 100, y);

printf("%s", y);

Now, its retira_espaco function is erroneously declared as int retira_espaco , should be void retira_espaco . Also it is not using the 3rd parameter t , and that final command return (retorno) is unnecessary and in fact wrong, it would only make sense if the function type was char * .

In addition, your printf("%s\n", retira_espaco(tamanho, vetor, tamanho, retorno)); instance is wrong because the %s format is for strings (char *), and you declared the function retira_espaco as int.

    
12.09.2016 / 03:45
2

Let's see ...

The best way to deal with your problem is to use the passing by reference idea of the arguments. And that falls into that black cloud of pointers - but that's easy once you master!

If you want to read about pointers, I suggest this USP material . It is short, concise and easy to understand. I also indicate this question from Stack Overflow in English if you master the language.

Solving the problem

Taking the code you provided as true and making it lean for the purpose of exemplification, what could be done is as follows:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void retira_espaco( char * vetor ){

  int i, j, tamanho;

  tamanho = strlen( vetor );

  for ( i = 0; ( i < tamanho ) && ( vetor[i] != 0 ); i++ ){

    if( isdigit( vetor[i] ) || vetor[i] == 'i' || vetor[i] == 'p' || vetor[i] == '+' || vetor[i] == '-' || vetor[i] == '*' || vetor[i] == '/' || vetor[i] == '^' || vetor[i] == '=') {

      /* Movimenta para posteriores posições para dentro, eliminando caracteres desejados */
      for( j = i; j < (tamanho - 1); j++ ) vetor[j] = vetor[j+1];
      vetor[ tamanho - 1 ] = '
Resultado: abcdefefghjklmnoqxyz
'; /* Por conta do movimento acima, precisa-se checar a posição atual novamente */ --i; } } } int main( void ){ char vetor[] = "abcdef+=efghijklmnopq/xyz"; retira_espaco( vetor ); printf( "Resultado: %s\n", vetor ); return( 0 ); }

The result is satisfactory:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void retira_espaco( char * vetor ){

  int i, j, tamanho;

  tamanho = strlen( vetor );

  for ( i = 0; ( i < tamanho ) && ( vetor[i] != 0 ); i++ ){

    if( isdigit( vetor[i] ) || vetor[i] == 'i' || vetor[i] == 'p' || vetor[i] == '+' || vetor[i] == '-' || vetor[i] == '*' || vetor[i] == '/' || vetor[i] == '^' || vetor[i] == '=') {

      /* Movimenta para posteriores posições para dentro, eliminando caracteres desejados */
      for( j = i; j < (tamanho - 1); j++ ) vetor[j] = vetor[j+1];
      vetor[ tamanho - 1 ] = '
Resultado: abcdefefghjklmnoqxyz
'; /* Por conta do movimento acima, precisa-se checar a posição atual novamente */ --i; } } } int main( void ){ char vetor[] = "abcdef+=efghijklmnopq/xyz"; retira_espaco( vetor ); printf( "Resultado: %s\n", vetor ); return( 0 ); }

Explaining

If you create a pointer - I guess you know what a pointer is or you read the recommended article - as a parameter to the function (example: char * vetor ), you can pass your vetor array as a reference to the function - that is, the value should not be passed, but the memory address of the same. This means that you can modify the contents of the original array.

What was done here is simple:

  • The function that modifies the array was created: void retira_espaco( char * vetor ){...}

  • A predetermined array was modified by reference: retira_espaco( vetor );

  • It was verified that the modification occurred: printf( "Resultado: %s\n", vetor );

Note that vectors are passed by reference by default. If it were an allocated vector, we would use the & operator to reference. Example:

  • function would be: void retira_espaco( char ** vetor ){...}
  • the array declaration in main() would be: char **vetor = malloc(...);
  • passing by reference would look like: retira_espaco( &vetor );

In this way, one could not only maintain the values as well as modify the allocated memory size. With dynamic memory allocation, instead of filling with '\ 0', it would obtain an array of appropriate size and with less memory consumption. However, it is a bit more laborious and goes beyond the scope of presentation of the theme.

    
12.09.2016 / 05:56