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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:38:25+00:00 2026-06-18T07:38:25+00:00

I am sorry that I cannot support my question with some code (I didnt

  • 0

I am sorry that I cannot support my question with some code (I didnt understand how to structure it so it would be accepted here), but I try anyway.

If I understand correctly, a struct that references a struct of same type would need to do this with contained pointer for reference. Can this pointer reference to allocated space on the stack (instead of the heap) without creating segmentation fault? –

how should this be declared?

  • 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-18T07:38:26+00:00Added an answer on June 18, 2026 at 7:38 am

    Yes, you can use pointers to variables on the stack, but only when the method that provides that stack frame has not returned. For example this will work:

    typedef struct
    {
        int a;
        float b;
    } s;
    
    void printStruct(const s *s)
    {
        printf("a=%d, b=%f\n", s->a, s->b);
    }
    
    void test()
    {
        s s;
        s.a = 12;
        s.b = 34.5f;
        printStruct(&s);
    }
    

    This will cause an error however, as the stack frame would have disappeared:

    s *bad()
    {
        s s;
        s.a = 12;
        s.b = 34.5f;
        return &s;
    }
    

    EDIT: Well I say it will cause an error, but while calling that code with:

    int main()
    {
        test();
        s *s = bad();
        printStruct(s);
        return 0;
    }
    

    I get a warning during compilation:

    s.c:27:5: warning: function returns address of local variable [enabled by default]
    

    and the program appears to work fine:

    $ ./s
    a=12, b=34.500000
    a=12, b=34.500000
    

    But it is, in fact, broken.

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

Sidebar

Related Questions

first of all, sorry if that question is dumb but I´m a total newbie
I'm sorry, I'm sure that this will be simple but I just cannot see
Sorry that this is a rather large question. I cannot get the following C++
This is not the clearest of question titles, sorry about that, but I will
Sorry that I haven't done much of my own research but I do not
Sorry that I can't find the answer through google - But I know this
I'm sorry that I haven't deeply understood HBase and Hadoop MapReduce, but I think
This question will be a little longer and I am sorry for that :)
Sorry if the title is not that specific, but I don't know how else
(I'd be very sorry to hear this is a stupid question, but I've done

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.