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

  • SEARCH
  • Home
  • 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 7080341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:47:01+00:00 2026-05-28T06:47:01+00:00

I need to get a string without asking for a length, I create a

  • 0

I need to get a string without asking for a length,
I create a buffer of 100 char and when is full I do a realloc to add a space for a char at the end of the string

this is my code…could you help me?

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

int main(int argc, char **argv)
{   
    char *content = malloc(10*sizeof(char));
    char c;
    content[0]='\0';

    while ((c = getchar()) != EOF)
    {
        if (strlen(content) < 10){
            strcat(content, &c);
            content[strlen(content)+1] = '\0';
        }
       else {
            content=realloc(content,sizeof(char)*(strlen(content))+2);
            strcat(content, &c);
            content[strlen(content)+1] = '\0';
        }
    }
    printf("%s",content);       

    return 0;
}
  • 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-05-28T06:47:01+00:00Added an answer on May 28, 2026 at 6:47 am

    A few issues here:

    1. don’t use strcat like that! You should pass a pointer to a \0 terminated string, instead of a pointer to a single char. This only works by accident.
    2. Keep track of the allocated memory size in a separate variable, instead of using strlen().
    3. Keep track of the position in the string using another separate variable, not by calling strlen() constantly. Set the terminating \0 once, after the loop is done.
    4. a better reallocation strategy would be to grow memory allocation in chunks, i.e. not for every byte. You could allocate X bytes every time your allocated memory is exhausted, or you could double the number of allocated bytes.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get all substrings from string. For ex: StringParser.GetSubstrings([start]aaaaaa[end] wwwww [start]cccccc[end], [start],
I try create method for get the connection string value but without value for
I have this string: 123-456-7 I need to get this string: 1234567 How I
I'm using Fluent NHibernate and need to get my Connection String from the connection.connection_string
I need to get it as a string to use elsewhere in the program,
I need to get the last character of a string. Say I have testers
I need a quick easy way to get a string from a file in
For eg., i have alphanumeric string 'ABCDEF 0 0.450' and i need to get
I need a regular expression to basically get the first part of a string,
How can I get some part of string that I need? accountid=xxxxxx type=prem servertime=1256876305

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.