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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:22:58+00:00 2026-05-30T09:22:58+00:00

Something I stumbled upon and made me wonder. Why does this work? void foo

  • 0

Something I stumbled upon and made me wonder.
Why does this work?

void foo (int* a)
{
    int x = 3;
    *a = x;
}

int main()
{
    int a;
    foo(&a);
    return 0;
}

But this causes a segmentation fault (both on Visual Studio 2008 and gcc)?

void foo (int* a)
{
    int x = 3;
    *a = x;
}

int main()
{
    int* a;
    foo(a);
    return 0;
}

Is it something defined in the language or just an implementation issue?

  • 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-30T09:23:00+00:00Added an answer on May 30, 2026 at 9:23 am

    When you declare

    int* a;
    

    You are declaring a pointer variable a but you are not making it point to anything. Then in the function, you do

    *a = x;
    

    Which dereferences the pointer and tries to assign what it points to the value of x. But since it doesn’t point to anything, you get undefined behaviour, manifested in a segmentation fault.

    You should do this:

    int i; // the actual integer variable
    int* a = &i; // a points to i
    

    The difference between that and the first one is that int a; declares a real integer variable, then you take its address with &a and passes it to the function. The pointer a inside the function foo points to the variable a in main, and so dereferencing it and assigning to it is perfectly fine.

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

Sidebar

Related Questions

I asked a similar question before ( integer-vs-char-for-db-record-property ) but stumbled upon something that
I've just stumbled upon this problem on the net. It kind of goes something
I was looking up something, and stumbled upon this code: http://google.com/codesearch?q=kBrowserThreadNames Where can I
On my reading spree, I stumbled upon something called Intentional Programming . I understood
I'm trying out something in jQuery and stumbled upon a site http://pupunzi.com/ , on
While looking for something totally different the other day, I have stumbled upon two
I'm reviewing java and stumbled upon something like the following block of code public
I have been going over rails scheduling tasks options and stumbled upon this piece
I'm trying to understand the principles of the Repository question. But i've stumbled upon
I stumbled upon this post In Objective-C why should I check if self =

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.