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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:57:52+00:00 2026-06-04T08:57:52+00:00

I know why this works: #include <stdio.h> void cool_number(int **number) { int value =

  • 0

I know why this works:

#include <stdio.h>

void cool_number(int **number) {
  int value = 42;
  int *p = &value;
  *number = p;
}

int main () {
  int *number;
  cool_number(&number);
  printf("number is %d\n", *number);
  return 0;
}

What I don’t understand is why this doesn’t (in my machine it prints 3700 or something like that).

#include <stdio.h>

void cool_number(int **number) {
  int value = 42;
  int *p = &value;
  int **x = &p;
  number = x;
}

int main () {
  int *number;
  cool_number(&number);
  printf("number is %d\n", *number);
  return 0;
}

Why aren’t both equivalent?

  • 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-04T08:57:54+00:00Added an answer on June 4, 2026 at 8:57 am

    I assume they’re not equivalent because number is passed by value, on the stack, as is standard for function parameters. Any changes that you make directly to number inside of cool_number() are modifying the local copy on the stack, and are not reflected in the value of number in main().

    You get around this in the first example by dereferencing number, which tells the computer to modify some specific location in memory that you also happen to have a pointer to back in main(). You don’t have this in the second example, so all that happens is that you make the local number pointer point to somewhere else, without actually updating any memory location being referred to back in main(). Thus nothing you do shows up once you get back to main().

    And since value is local to the cool_number() function, setting a reference to it that will be accessed after cool_number() returns isn’t guaranteed to work and certainly shouldn’t be used in any code outside of a trivial/toy example. But in this specific instance it’s not really related to why you’re seeing different results between the two pieces of code.

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

Sidebar

Related Questions

Consider this program: #include <stdio.h> int main() { printf(%s\n, __FILE__); return 0; } Depending
I have a very simple print program called print.c: #include <stdio.h> int main(void){ printf(Random
I have this piece of code and i don't know how it works #include
Consider this code: #include <stdio.h> #define N 5 void printMatrix(int (*matrix)[N],int n) { int
#include <stdio.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; curl = curl_easy_init();
My code #include <stdio.h> #include <stdlib.h> #include <ctype.h> void getData(short int *number, char *string)
int main(void) { char *input; printf(prompt>); scanf(%s, input); printf(%s, input); return 0; } prompt>input
The code: #include stdafx.h #include stdio.h #include math.h #include stdlib.h #include time.h int main()
#include <stdio.h> typedef struct pduct {char name[20]; int price; int stock;} PRODUCT; void init(PRODUCT
I am getting to know boost::variant. I think this example should work. #include <boost/fusion/sequence.hpp>

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.