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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:49:54+00:00 2026-05-26T00:49:54+00:00

I always get a segmentation fault during execution of this test program. I can’t

  • 0

I always get a segmentation fault during execution of this test program. I can’t figure out why. Maybe someone could explain it to me please, i’m sure i mixed up the pointer stuff.

#include <stdio.h>

struct xy {
    int         (*read)();
    void        (*write)(int);
};

struct z {
    struct xy    *st_xy;
};


static void write_val(int val)
{
    printf("write %d\n", val);
}

static int read_val()
{
    /* return something just for testing */
    return 100;
}

int init(struct xy *cfg)
{
    cfg->read = read_val;
    cfg->write = write_val;
    return 0;
}

int reset(struct z *st_z)
{
    /* write something just for testing */
    st_z->st_xy->write(111);

    return 55;
}

int main(int argc, char **argv)
{
    static struct z test;
    int ret;
    int ret2;

    ret = init(test.st_xy);
    printf("init returned with %d\n", ret);

    ret2 = reset(&test);
    printf("reset returned with %d\n", ret2);

    return 0;
}
  • 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-26T00:49:54+00:00Added an answer on May 26, 2026 at 12:49 am

    You never allocate the actual xy object. Your test.st_xy is just a garbage pointer that you’re not allowed to dereference.

    Instead, do something like this:

     static struct z test;
     static struct xy inner_test;
     test.st_xy = &inner_test;
    
     // ...
    
     ret = init(test.st_xy);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Android app, I always get VerifyErrors! And I cannot figure out why.
I always get this error when trying to compile my file with Boost::GIL PNG
I am trying to deserialize a stream but I always get this error End
I always get confused between the static and dynamic scoping and hence need someone
I always get the undefined index notice, when I run this on wampserver. Tried
This js loop script always get the last value of ui_item inside a jquery
I always get confused with licenses, I'm reading up again, but I'm sure someone
I'm debugging my C++ Win32 program in VS2010 and I always get Windows has
I always get confused. Would someone explain what Reentrant means in different contexts? And
I get segmentation fault accessing an object which looks valid and fully accessible in

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.