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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:09:12+00:00 2026-06-18T06:09:12+00:00

With this structure definition: struct word { char *cont; //content of the word char

  • 0

With this structure definition:

struct word {
    char *cont; //content of the word
    char *wsp; //whitespace following the word
    int ctr;
};

for a word, I’m trying to write a function to get the first word from stdin with all the whitespace following it. Here it is:

struct word *getword(){
    char cont[WORDLIM];
    char wsp[WORDLIM];
    cont[0] = '\0';
    wsp[0] = '\0';
    if (peekchar() == EOF) return NULL; //peekchar defined elsewhere as getting a char and ungetc-ing it

    REPEAT{ //macro for for(;;)
            char c = getchar();
            char buf[2];
            buf[0]=c;
            buf[1]='\0';
            if (c == '\n' || c == ' '){
                    strcat(wsp, buf);
                    if (peekchar() != '\n' && peekchar() != ' '){
                            struct word *toret;
                            toret = malloc(sizeof(struct word));
                            toret->cont = cont;
                            toret->wsp = wsp;
                            toret->ctr = -1;
                            printf("---%s---\n", toret->wsp); //just for debugging
                            return toret;
                    }
                    continue;
            }
    }
    printf("PANIC PANIC PANIC THIS IS GOING WROOOOONG!!!!!\n");
    return NULL;
}

Now the fun thing is that I can get the correct output of whitespace in the just for debugging line, but when I attempt to access getword()->wsp, I get random garbage. Even more interestingly, getword()->cont works…

I’m a really new newbie to C…what did I do wrong?

  • 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-18T06:09:13+00:00Added an answer on June 18, 2026 at 6:09 am
    REPEAT{ //macro for for(;;)
    

    Lose that macro post-haste. Using the preprocessor to de-uglify C syntax is a recipe for heartburn. Just use for(;;) or while(1). The compiler is usually smart enough to turn it into an unconditional jump.

    As for your problem:

    toret->cont = cont;
    toret->wsp = wsp;
    

    The cont and wsp arrays are declared locally to the getword function; once the function exits, the arrays cease to exist, and any pointers to them are no longer valid. What you’ll need to do is allocate memory for the cont and wsp members of toret in addition to the memory for toret itself.

    toret = malloc(sizeof(struct word));
    if (toret) // ALWAYS check the result of a `malloc` call
    {
      toret->cont = malloc(strlen(cont) + 1);
      if (toret->cont)
        strcpy(toret->cont, cont);
      toret->wsp = malloc(strlen(wsp) + 1);
      if (toret->wsp)
        strcpy(toret->wsp, wsp);
      ...
    }
    

    Note that when you need to free the memory for the struct, you’ll first need to free the memory for the cont and wsp members.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My structure definition is, typedef struct { int taxid; int geneid; char goid[20]; char
I am having this structure in a .c file: struct entry { int position;
Recently I came across a structure definition, struct arr { int cnt; struct {
I have following query: THis is my structure in some .h file typedef struct
Consider this piece of code: struct Trade { float Price; char* time; int shares;
I have this definition for my structure: struct localframevelo { double ivelo; //i(x) component
I have this Structure the elements of which that I need to write in
I have the following template structure: template <typename scalar_type> struct postc_params{ scalar_type delta; unsigned
The definition of GUID in the windows header's is like this: typedef struct _GUID
int main() { // forward declaration struct myStruct_st *mS; // Note that this will

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.