How to return a char vector to a function in C? type as a pointer

Asked

Viewed 3,199 times

0

Hello, I’m trying to return a vector that receives the content of a matrix in a function, but it’s not working, I appreciate if someone can help me, I’m in doubt

Function call on main:

char vetor[tamMsg*9];
retornar_vetor(&mensagem, &vetor);

The function:

char*  retornar_vetor(char **mensagem, char **vet) {
    int i, j;
    char cpy[2];
    int tamanhoMsg = strlen(*mensagem);
    char str[tamanhoMsg][9];


    for(i=0; i<tamanhoMsg; i++){
        str[i][0] = '\0';
        for(j=0; j<8; j++){
            sprintf(cpy,"%d", (*mensagem)[i]%2);
            strcat(str[i], cpy);
            vetor[j] = str[i]
            (*mensagem)[i]/=2;
        }
        str[i][j] = '\0';
        printf("%s", str[i]);
    }

return vet;
    printf("\n");
}

Note: The focus of the question is to be able to put the values of the matrix str in the vector and return to the vector in main, so the pointer pointer, I’m not able to understand how to do

  • I don’t understand the doubt. Are you having any problems? As the rest, the problem may be how you are consuming. Anyway I’m pretty sure that’s been answered before.

2 answers

1

Following its original rationale, follows an example (tested) of how to concatenate and return each message contained in another vector of pointers (messages):

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


void retornar_vetor( char ** mensagem, char ** ret )
{
    int i = 0;

    while( mensagem[i] )
        strcat( *ret, mensagem[i++] );
}


int main( int argc, char ** argv )
{
    char * msgs[] = { "Alpha", "Beta", "Gamma", "Delta", "Episilon", "Zeta", "Eta", "Theta", NULL };
    char vetor[100] = {0};

    char * paux = &vetor[0];

    retornar_vetor( msgs, &paux );

    printf("%s\n", vetor );

    return 0;
}

Exit:

$ ./vetor_v1 
AlphaBetaGammaDeltaEpisilonZetaEtaTheta

There is a more elaborate way to solve the problem with dynamic memory allocation with the functions malloc(), realloc() and free(), follows another example (tested):

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

void retornar_vetor( char ** mensagem, char ** ret )
{
    int i = 0;
    char * p = NULL;
    size_t len = 0L;

    p = (char*) malloc( sizeof(char) );

    *p = 0;

    if( !mensagem )
    {
        *ret = p;
        return;
    }

    while( mensagem[i] )
    {
        len += strlen( mensagem[i] );

        p = realloc( p, len + 1 );

        strncat( p, mensagem[i], len );

        i++;
    }

    *ret = p;
}


int main( int argc, char ** argv )
{
    char * msgs[] = { "Alpha", "Beta", "Gamma", "Delta", "Episilon", "Zeta", "Eta", "Theta", NULL };

    char * vet = NULL;

    retornar_vetor( msgs, &vet );

    printf("%s\n", vet );

    free(vet);

    return 0;
}

Exit:

$ ./vetor_v2 
AlphaBetaGammaDeltaEpisilonZetaEtaTheta

0

The variable "vet" is of the pointer type, so you should return the same type.

Ex:

char  **retornar_vetor(char **mensagem, char **vet){

   .
   .
   .
   .

   return vet;

}

Upon receiving function value you should also store in a variable of the same type.

Browser other questions tagged

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