polslinux
polslinux

Reputation: 1779

Split main into one function

This is my code:

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

int main()
{
        char c, *data, *temp;
        data=malloc(sizeof(char));
        int i, conta=0;
        printf("Write hostname: ");
        for (i=0;;i++) {
            c=getchar();
            conta++;
            if (c=='\n') break; /* Se premo invio esco dal ciclo */
            data[i]=c; /* Inserisco il carattere nell'array */
            temp=realloc(data,(i+2)*sizeof(char)); /* do memoria aggiuntiva al puntatore (+2 per carattere NUL) */
            if ( temp != NULL ) {
                    data=temp; /* Se la riallocazione va a buon fine assegno temp a data */
            } else {
                    free(data); /* altrimenti libero il puntatore, stampo messaggio errore e chiudo programma */
                    printf("Error allocating memory!\n");
                    return EXIT_FAILURE;
            }    
        }
    /* Stampo la stringa letta, libero puntatore e chiudo programma */
    for (i=0;i<=conta;i++){
            printf("%c", data[i]);
        }
        printf("\n");
        free(data);
        return EXIT_SUCCESS;
}

I would like to split my main into a function that will get user input (hostname, username).
I would like something like:

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

function read_input();

int main()
{
    printf("Write hostname: ");
    read_input();
    printf("Write username: ");
    read_input();
    for (i=0;i<=conta;i++){
      printf("%c", data[i]);
    }
    printf("\n");
    free(data);
    return EXIT_SUCCESS;
}

read_input(){
....
}

but i don't know how i have to start to make this function...
I don't know how to make a function that will return conta (for the for cicle), the pointer data (which must be freed and it is needed to print the output).
Have i to make a struct and pass it to the return into the function?

Upvotes: 0

Views: 106

Answers (1)

Thomash
Thomash

Reputation: 6379

You can organize your code this way:

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

int read_input(char data[]);

int main()
{
    char *data;
    int i, conta;
    data=malloc(sizeof(char));
    printf("Write hostname: ");
    conta = read_input(data);
    /* Stampo la stringa letta, libero puntatore e chiudo programma */
    for (i=0;i<=conta;i++){
        printf("%c", data[i]);
    }
    printf("\n");
    free(data);
    return EXIT_SUCCESS;
}

int read_input(char data[])
{
    char c, *temp;
    int i, conta = 0;
    for (i=0;;i++) {
        c=getchar();
        conta++;
        if (c=='\n') break; /* Se premo invio esco dal ciclo */
        data[i]=c; /* Inserisco il carattere nell'array */
        temp=realloc(data,(i+2)*sizeof(char)); /* do memoria aggiuntiva al puntatore (+2 per carattere NUL) */
        if ( temp != NULL ) {
                data=temp; /* Se la riallocazione va a buon fine assegno temp a data */
        } else {
                free(data); /* altrimenti libero il puntatore, stampo messaggio errore e chiudo programma */
                printf("Error allocating memory!\n");
                return EXIT_FAILURE;
        }
    }
    return conta;
}

Upvotes: 1

Related Questions