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

The Archive Base Latest Questions

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

int d() {return 0;} int i() {return 7;} struct a { int(*b)(); }c={d}; typedef

  • 0
int d() {return 0;} int i() {return 7;}

struct a { int(*b)(); }c={d};

typedef struct e{ struct a f; }g;

main() { struct e *h; h->f.b = i; }

I am getting segmentation fault when I try to run this program. Can anyone justify the reason?

And I also tried like

int d() {return 0;} int i() {return 7;}

struct a { int(*b)(); }c={d};

typedef struct e{ struct a f; }g;

main() { struct e *h; h = (g)malloc(sizeof(g)); h->f.b = i; }

Now I am getting errors like

funptrinstrct.c: In function `main': funptrinstrct.c:17: error: conversion to non-scalar type requested

Answer for this also would be appreciable.

  • 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-16T08:15:34+00:00Added an answer on May 16, 2026 at 8:15 am

    For the first question, you create a pointer h without initialising it, then you immediately try to dereference it with h->f.b.

    For the second one, you should be casting to g*, not g:

    #include <stdio.h>
    
    int d (void) { return 0; }
    int i (void) { return 7; }
    
    struct a { int(*b)(void); } c = {d};
    typedef struct e { struct a f; } g;
    
    int main (void) {
        struct e *h = (g*)malloc (sizeof (g));
        h->f.b = i;
        printf ("%d\n", h->f.b());
    }
    

    That’s because g is a structure, not a pointer to a structure. The code above outputs 7 as expected.

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

Sidebar

Related Questions

When I try to execute this program I am getting segmentation fault. What could
Can you please tell me what I did wrong? I'm getting SIGSEGV (Segmentation fault)
I am getting a segmentation fault when I run prog.c. I have found that
why doesn't this compile: enum E { a, b} typedef struct { int i;
Is this code correct? std::function<int(int)> f = [&f](int n) -> int { return n
How can i do some thing like: #include <functional> #include <boost/functional.hpp> int foo(int){return 1;};
int main() { return 0; } const uint8_t* byteHunter(const uint8_t *Search, uint16_t sLength, const
test.c: int main() { return 0; } I haven't used any flags (I am
It should turn this int Yada (int yada) { return yada; } into this
Is is possible in C to create structs inline? typedef struct { int x;

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.