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 6873137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:01:16+00:00 2026-05-27T04:01:16+00:00

I need an array of strings in which the length of the array is

  • 0

I need an array of strings in which the length of the array is not known at compile time. what I’ve done is:

char **words;

words = malloc(capacity * sizeof(char *));

char nextword[MAX_LEN + 1];

while (true) {
    if (num_entries == capacity) {
        capacity += 5;
        realloc(words, (sizeof(char *) * capacity));
    }

    printf("\nEnter a word: ");
    fgets (nextword, MAX_LEN, stdin);

    remove_newline(nextword);

    if (strlen(nextword) == 0) break;

    words[num_entries] = malloc(strlen(nextword + 1));

    if (words[num_entries] == NULL) {
        printf("\nout of space\n");
        break;
    }

    strcpy(words[num_entries], nextword);
    num_entries++;

this seems to work to expand the size once, except that the first element after the expansion has become NULL for some reason. The second time realloc is executed I get an error:

"invalid next size".

  • 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-27T04:01:16+00:00Added an answer on May 27, 2026 at 4:01 am

    Your code is almost there, just need a few modifications. An important thing to remember is that realloc does not modify the value that you pass to it, and it is not required to return the pointer to the same chunk of memory that you passed to it. Here is a working example of using realloc. It is rather straightforward, so you should be able to fix your code by simply following the example.

    char **more_words = realloc(words, capacity);
     if (more_words) {
         words = more_words;
     } else {
         // Do something about realloc failure
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I needed to split a string of numerous words into an array which
I need to serialize an object array array<object^>^ in C++/CLI which contains one intPtr
I have strings "A função" , "Ãugent" in which I need to replace characters
I have an array of char pointers of length 175,000. Each pointer points to
Writing a program in which I need to split strings from a struct linked
I need just dictionary or associative array string => int . There is type
I need to search a string in the string array. I dont want to
I need an advice about how to create new string array from 4 different
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I need to create an array by searching a string for occurrences of '['

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.