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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:43:47+00:00 2026-06-15T23:43:47+00:00

I define a struct data type: typedef struct LinkNode LinkNode; struct LinkNode { char

  • 0

I define a struct data type:

typedef struct LinkNode LinkNode;
struct LinkNode {
    char *name;
    LinkNode *next;
};

And call it in main():
Example1:

int main()
{
    LinkNode *pnode = (LinkNode *) malloc(sizeof(LinkNode));

    scanf("%s", pnode->name);
    ...
    free(pnode);
    return 0;
}

It doesn’t work, unless add one line: Example 2

int main()
{
    LinkNode *pnode = (LinkNode *) malloc(sizeof(LinkNode));
    pnode->name = (char *) malloc(sizeof(char));

    scanf("%s", pnode->name);
    ...
    free(pnode);
    return 0;
}

But the following code works: Example 3

int main()
{
    LinkNode *pnode = (LinkNode *) malloc(sizeof(LinkNode));

    pnode->name = "Jim";
    ...
    free(pnode);
    return 0;
}

So what’s the problem? I have been assigned memory to pnode, why scanf() doesn’t work? I’m using VS2010.
Thanks!

  • 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-15T23:43:48+00:00Added an answer on June 15, 2026 at 11:43 pm

    When you use scanf, you need an allocated block of memory to write to. When you allocate memory for name with malloc it works, even if you are just allocating one byte (like @billz pointed out), because you have a valid memory address now. You have to allocate enough memory though, or you will overwrite something and cause trouble sooner or later.

    pnode->name = (char *) malloc(sizeof(char) * <max size of input>);
    

    When you assign a string literal to the pointer name (pnode->name = "Jim";), you are merely setting it to the address of a hardcoded string in memory. If you try to modify it bad things will happen, so never do this unless you are using a const pointer.

    Bottom line, name is just a pointer, to do anything with it, you need it to point to something valid.

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

Sidebar

Related Questions

I have the code: main() { typedef struct { int data; } Information; typedef
I'm using a struct like this: define struct _Fragment{ int a; char *seq; }Fragment;
struct { char a; int b; } x; Why would one define a struct
I have: struct elem { data d; struct elem *next; }; typedef struct elem
Given the following structure: struct nmslist_elem_s { nmptr data; struct nmslist_elem_s *next; }; typedef
I have a struct defined as: typedef struct { int type; void* info; }
I have this struct; #define BUFSIZE 10 struct shared_data { pthread_mutex_t th_mutex_queue; int count;
#define MAX 100 struct bs{ int ab; int ac; }be; struct s{ be b;
Is is possible to define a macro BREF(...): struct bits { int b0:1; int
I'm curious why C++ does not define void via : typedef struct { }

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.