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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:33:58+00:00 2026-05-16T18:33:58+00:00

A trick question about C pointer. Read code snippet below and try explain why

  • 0

A trick question about C pointer. Read code snippet below and try explain why the list value changed (this question was based on this code):

tail has the memory address of list.

How is possible list be changed below?

typedef struct _node {
   struct _node *next;
   int value;
}Node;


 int main(){
   Node *list, *node, *tail;
   int i = 100;

   list = NULL;
   printf("\nFirst . LIST value = %d", list);

  tail =(Node *) &list;
  node = malloc (sizeof (Node));
  node->next = NULL;
  node->value = i;

  //tail in this point contains the memory address of list
  tail->next = node;
  printf("\nFinally. LIST value = %d", list);
  printf("\nLIST->value = %d", (list->value));

return 0;

}

—- Output

First . List value = 0

why this values ??? im not expecting this …

Finally . LIST value = 16909060

LIST->value = 100

  • 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-16T18:33:59+00:00Added an answer on May 16, 2026 at 6:33 pm

    Let’s look at what happens to the memory in your program. You start with 3 local variables, all of type Node*. At the moment they all point to garbage, as they have been declared but not initialised.

    An ascii art diagram of the memory might be (The layout is implementation dependant)

          list   node   tail
      --------------------------
    ... | 0xFE | 0x34 | 0xA3 | ...
      --------------------------
    

    You then set list to NULL, and tail to the address of node (casting away its type, a bad idea), giving you

          list   node   tail
      --------------------------
    ... | NULL | 0xFE | &list | ...
      --------------------------
            ^             |
            +-------------+
    

    You then malloc a new Node, setting list to its address.

          list    node   tail          next  value
      ---------------------------  ------------------
    ... | NULL | &next | &list | ... | NULL | 100 | ...
      ---------------------------  ------------------
            ^      |       |             ^
            |      +---------------------+
            +--------------+
    

    You next try to set tail->next to node. You’ve said that you know tail points to a Node when you did the typecast, so the compiler believes you. The Node tail points to starts at list‘s address, like so

         tail                                list
         next    value                       next   value
      ----------------------------------  ------------------
    ... | NULL | &list->next | &list | ... | NULL | 100 | ...
      ----------------------------------  ------------------
    

    You then set tail->next to node, making both list and node point to the list structure.

          list    node   tail          next  value
       ---------------------------  ------------------
    ... | &next | &next | &list | ... | NULL | 100 | ...
       ---------------------------  ------------------
           | ^     |       |             ^
           | |     +---------------------|
           | +-------------+             |
           +-----------------------------+
    

    You’ve printed list as a signed integer (“%d”). This is a bad idea – if you are using a 64 bit machine and have other arguments in the printf statement they may be clobbered, use the pointer format (“%p”) instead. list->value is the same as node->value, so it’s still going to be 100.

    Pointers become easier if you think about how they actually are represented in the machine – as an index to a huge array which holds all of your data (modulo pointer sizes, virtual memory etc.).

    Next time it might be easier just to use list = node.

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

Sidebar

Related Questions

I asked a question about this previously but my database structure has changed, and
Following the CSS style trick from this question I was able to create a
Inspired by this question , I'd like to know whether there is any trick
Context This is a question mainly about Lucene (or possibly Solr) internals. The main
This particular question is more about trying to see the reason behind compiler message
I was intrigued by this answer to my question about getting vim to highlight
Reading to this question i've just learned the existence of the blackhole table trick:
I have a sort of theoretical question about freeing a pointer. I think some
Another question about SVG style transitions... :) This time I'm trying to transition the
I am new to D3 and just had a quick question about tick labels

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.