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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:56:55+00:00 2026-06-14T19:56:55+00:00

I need to create program that takes input from stdin in this format: abcde

  • 0

I need to create program that takes input from stdin in this format:

abcde //number of characters in word = number of words => square shape
fghij
klmno
pqrst
uvwxy
        // \n separates first half from second
word1word //any amount of characters, any amount of words
word
word2
sdf
        // \n to end input

My code works, but only about 50% of the time. I have couple of example inputs, that I use for testing, but for some of them my readwords function fails.

Here is my function, that reads words. Since I have no idea how many words or how long they are going to be, I use dynamic arrays and getchar() function.

void readWords(char **p,int *n,int w) /* before calling: n = 50; w = 20; p = 50x20 char array */
{
int i = 0,j = 0,x;
char tmp,prevtmp;

while (1)
{
    prevtmp = tmp;
    tmp = getchar();
    if ((prevtmp == '\n'  && tmp == '\n') || feof(stdin))
        break; /* no more words to read */

    if (tmp == '\n') /* end of word */
    {
        p[i][j] = '\0'; /* add \0 to create string format */

        i++;
        j = 0;
        if (i == *n) /* if there is more words than there is space for them, double the size */
            if (realloc(p,*n*2) != NULL)
                *n*=2;

        continue;
    }

    p[i][j] = tmp;
    j++;
    if (j == w) /* if width of word is larger than allocated space, double it */
    {
        for (x = 0; x < *n;x++);
            if(realloc (p[x],w*2) != NULL);

        w=w*2;
    }

}
*n = i;
}

This is example of input for which this works (note:this function only reads second half after line with only \n):

dsjellivhsanxrr
riemjudhgdffcfz
<skipping>
atnaltapsllcelo
ryedunuhyxhedfy

atlanta
saltlakecity

<skipping 15 words>

hartford
jeffersoncity

And this is input that my function doesn’t read properly:

<skipping>
...oywdz.ykasm.pkfwb.zazqy...
....ynu...ftk...zlb...akn....

missouri
delaware

<skipping>

minnesota
southdakota

What my function reads from this input:

e
yoming
xas
florida
lvania
ana
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

There is no difference between those two inputs (except different words and different amount and length of words), the first half gets read properly no matter what, but only the second half bugs out. How do I fix this?

P.S. sorry for long post, in case you want to see full input without skipped bytes, here is pastebin: http://pastebin.com/hBGn2tej

  • 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-14T19:56:56+00:00Added an answer on June 14, 2026 at 7:56 pm

    realloc() returns the address of the newly allocated memory, it does not update the argument passed into it. So this (and the other use of realloc()) is incorrect:

    if (realloc(p,*n*2) != NULL)
    

    and will results in the code accessing memory incorrectly, causing undefined behaviour. Store the result of realloc() to a temporary variable and check for non-NULL before updating p. The argument to realloc() also indicates the number of bytes, not the number of elements so the size argument calculation is incorrect as p is an array of char* so it should be realloc(p, sizeof(char*) * (*n * 2));. However, the change to p would not be visible to the caller. Also note that the only legal arguments to realloc() are pointers obtained from a previous call to malloc(), realloc() or calloc(). The comment p = 50×20 char array in the code suggests this is not the case.

    Here is a small example that allocates an array of char* which should be helpful:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    void f(char*** p)
    {
        /* Allocate space for two 'char*' elements.
           Add a NULL pointer element as sentinel value
           so caller knows where to find end of list. */
        *p = malloc(sizeof(**p) * 3);
    
        /* Allocate space for the two strings
           and populate. */
        (*p)[0] = malloc(10);
        (*p)[1] = malloc(10);
    
        strcpy((*p)[0], "hello");
        strcpy((*p)[1], "world");
        (*p)[2] = NULL;
    
        /* Add a third string. */
        char** tmp = realloc(*p, sizeof(**p) * 4);
        if (tmp)
        {
            *p = tmp;
            (*p)[2] = malloc(10);
            strcpy((*p)[2], "again");
            (*p)[3] = NULL;
        }
    }
    
    int main()
    {
        char** word_list = 0;
        f(&word_list);
    
        if (word_list)
        {
            for (int i = 0; word_list[i]; i++)
            {
                printf("%s\n", word_list[i]);
                free(word_list[i]);
            }
        }
        free(word_list);
    
        return 0;
    }
    

    Additionally:

    • prevtmp has an unknown value upon its first use.
    • getchar() actually returns an int and not a char.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create program that creates a XML schema like below using System.Xml.XmlSchema
I want to create a program that produces an executable slideshow. So I need
The folder I need to delete is one that is created from my program.
My Perl program takes some text from a disk file as input, wraps it
I am trying to create a linked list that will take the input from
Hello I have a Java application that takes an input number of operations to
I want to create a program that will take user input and create a
Here is my problem, I need to create a javascript function that takes 2
I have a program that creates multiple text files of rdf triples. I need
For a school project I need to create a program and it'd be nice

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.