Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7947861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:33:11+00:00 2026-06-04T01:33:11+00:00

This is my code: #include <stdio.h> #include <stdlib.h> int main() { char c, *data,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:33:13+00:00Added an answer on June 4, 2026 at 1:33 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<stdlib.h> int main() { char *temp; while(1) { temp= malloc(sizeof(char)*10); /*some code*/ }
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =
see my code is #include <stdio.h> #include<stdlib.h> #include<sys/stat.h> int main(int argc, char**argv) { unsigned
I have this very simple code: #include <stdio.h> #include <stdlib.h> int maxArr(int *arr) {
Is this code standard conforming ? #include <stdio.h> #include <cstdio> int main() { FILE
I got this C code. #include <stdio.h> int main(void) { int n, d, i;
Please look this code: #include <stdlib.h> #include <stdio.h> int N, L, I; float *
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I was under the impression that this code #include <windows.h> #include <stdio.h> int WINAPI

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.