Return string in C for handling outside the function where it was declared

2

I should develop a calculator that reads strings from the algebraic form of the operation with complex numbers. I need to manipulate the "main" vector outside the function in which it was declared (receives). How to proceed? Should I use pointers? How to use them?

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

int tam_max = 256;

int recebe(){

    int i = 0;
    int erro = 0;
    char principal[tam_max];
    char real1[tam_max];

    setbuf(stdin, NULL);
    fgets(principal, tam_max, stdin);

    for (i = 0; i < strlen(principal); i++){

        if (principal[0] == '\n'){
            erro++;
        } else if ( principal[i] == '+' || principal[i] == '-' ||
            principal[i] == '*' || principal[i] == '/' ||
            principal[i] == '=' || principal[i] == '^' ||
            principal[i] == 'i' || principal[i] == 'p' ||
            principal[i] == '0' || principal[i] == '1' ||
            principal[i] == '2' || principal[i] == '3' || 
            principal[i] == '4' || principal[i] == '5' || 
            principal[i] == '6' || principal[i] == '7' ||
            principal[i] == '8' || principal[i] == '9' || principal[i] == '\n'){
            erro == 0;
        } else {
            erro++;
        }
    } 
    return erro;
}

int validaDados(){

    int verifica = 0;
    verifica = recebe();

    if (verifica > 0){
        printf("\nCaracteres invalidos inseridos. Por favor, tente novamente.\n\n");
        validaDados();
    }

    if (verifica == 0){
        divide();
    }
}

int divide(){
    printf("OK ate aqui\n");
}

int main(){

    validaDados();

}
    
asked by anonymous 26.09.2016 / 06:10

2 answers

2

The right thing to do is always allocate the memory you need wherever you need it and pass that address on to whoever you manipulate. In exercises, the allocation of an array in the stack is not usually a problem. In more complex applications you may want to do dynamic allocation in heap .

The question does not clearly state how you're going to use it so I'm going to try something here by creating and using the string in validaDados() and pass it as an argument (such as #

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

#define tam_max 256

int recebe(char principal[tam_max]) {
    int erro = 0;
    setbuf(stdin, NULL);
    fgets(principal, tam_max, stdin);
    for (int i = 0; i < strlen(principal); i++) {
        if (principal[0] == '\n') {
            erro++;
        } else if ( principal[i] == '+' || principal[i] == '-' ||
            principal[i] == '*' || principal[i] == '/' ||
            principal[i] == '=' || principal[i] == '^' ||
            principal[i] == 'i' || principal[i] == 'p' ||
            principal[i] == '0' || principal[i] == '1' ||
            principal[i] == '2' || principal[i] == '3' || 
            principal[i] == '4' || principal[i] == '5' || 
            principal[i] == '6' || principal[i] == '7' ||
            principal[i] == '8' || principal[i] == '9' || principal[i] == '\n') {
            erro = 0;
        } else {
            erro++;
        }
    } 
    return erro;
}

void divide() {
    printf("OK ate aqui\n");
}

void validaDados() {
    int verifica = 0;
    char principal[tam_max];
    verifica = recebe(principal);
    printf("%s\n", principal);
    if (verifica > 0) {
        printf("\nCaracteres invalidos inseridos. Por favor, tente novamente.\n\n");
        validaDados();
    }
    if (verifica == 0) {
        divide();
    }
}

int main() {
    validaDados();
}

See running on ideone .

I've addressed some other issues. But I left other things that could get better. I do not know if the code does what it wants, but there is the solution asked for in the question.

    
26.09.2016 / 06:56
0

Here are some options for you:

Option 1 - The client allocates a buffer and passes its pointer as a parameter to be populated internally by the function:

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

#define MAXBUF  (100)

char * obter_msg( char * msg, size_t tam )
{
    strncpy( msg, "Ola Mundo!", tam );
    return msg;
}

int main( void )
{
    char msg[ MAXBUF + 1 ] = {0};
    obter_msg( msg, MAXBUF );
    printf("%s\n", msg );
    return 0;
}

/* OU... */

int main( void )
{
    char * msg = (char*) malloc( (MAXBUF + 1) * sizeof(char) );
    obter_msg( msg, MAXBUF );
    printf("%s\n", msg );
    free(msg);
    return 0;
}

Option 2 - The function has an internal static buffer and always returns its pointer to the client:

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

#define MAXBUF  (100)


char * obter_msg( void )
{
    static char msg[ MAXBUF + 1 ] = {0};
    strncpy( msg, "Olah Mundo!", MAXBUF );
    return msg;
}

int main( void )
{
    printf("%s\n", obter_msg() );
    return 0;
}

Option 3 - The function dynamically allocates a buffer and returns its pointer to the client (which in turn releases the buffer after use):

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

#define MAXBUF  (100)

char * obter_msg( void )
{
    char * msg = (char*) malloc( (MAXBUF + 1) * sizeof(char) );
    strncpy( msg, "Olah Mundo!", MAXBUF );
    return msg;
}

int main( void)
{
    char * msg = obter_msg();
    printf("%s\n", msg );
    free(msg);
    return 0;
}

I hope I have helped!

    
26.09.2016 / 17:00