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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:11:34+00:00 2026-06-01T07:11:34+00:00

I am unable to understand the weird behavior of this program. I have 2

  • 0

I am unable to understand the weird behavior of this program. I have 2 files, file1.c and file2.c

file.c is

#include <stdio.h>struct ll {
int key;
struct ll *next;
};
extern void func(struct ll*);

int main(void)
{
struct ll l = { 1, &l };
printf("%d %d\n",l.key,l.next->key);
func(&l);
return 0;
}

and file2.c is:

#include <stdio.h>

 struct ll 
 {
struct ll *next;    
int key;
 };


 void func(struct ll *l)
 {
   printf("%d \n",l->key);
   printf("%d \n",l->next->key);
 }

Now when I compile and run it, it shows segmentation fault. But where as in file2.c if I replace struct ll with :

struct ll 
{
 int key;   
 struct ll *next;       
};

Then it works fine. I mean just by interchanging the order of the declaration, it is impacting the output.

  • 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-01T07:11:35+00:00Added an answer on June 1, 2026 at 7:11 am

    The declaration of the struct should be the same both times, since struct is just a layout of data in memory, and you switch the variables.

    in your case, the code in the function func will try to dereference the integer 1 set in the main function. (or maybe do other weird things, as int and pointer are not compatible)

    In file.c:

    struct ll: [ int (key)   | pointer (next) ]
    struct ll l = { 1, &l }; // this causes:
    l:         [ 1           | &l             ]
    

    In file2.c:

    struct ll: [ pointer (next) | int (key)   ]
    // so the passed struct is treated in the same way:
    l:         [ 1           | &l             ]
                  next          key
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am unable to understand the weird behaviour of the program. It throws a
Recently I saw this piece of JavaScript code, but have been unable to understand
I am unable to understand this loop behavior of the javascript. Can someone tell
I have some questions about basic CSS that I was unable to understand or
I've done some searching, but I've been unable to fully understand how this is
I'm unable to understand the following codes $(this).animate({rotate3Di: degrees}, options) . Here rotate3Di is
I am unable to understand this why it prints the total binary bits instead
I have written a small program , to understand how futures work in c++0x.
This might be a dumb question but I'm unable to understand: When we create
I am trying this code on GNU's C++ compiler and am unable to understand

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.