My pointer/addresses are wrong no matter what I try












3















Here is what I'm trying:



int a,b;
int *ptr;

a = 123;
ptr = &a;
b = *ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("&ptr is %pn",&ptr);
printf("&b is %pn",&b);


Results in:



&a is 0x7ffee01fc828
ptr points to 0x7ffee01fc828
&ptr is 0x7ffee01fc818
&b is 0x7ffee01fc820


I expected &b to show the same address as &a... But that's not the case, so I tried:



int a,*b;
int *ptr;

a = 123;
ptr = &a;
b = ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("&ptr is %pn",&ptr);
printf("&b is %pn",&b);


This still results in an unexpected memory address for &b:



&a is 0x7ffee1cbd828
ptr points to 0x7ffee1cbd828
&ptr is 0x7ffee1cbd818
&b is 0x7ffee1cbd820


Can someone help me understand why I'm unable to get &b matching the same address as &ptr or &a?



Thanks!










share|improve this question


















  • 1





    you're comparing pointers to pointers... drop the & you'll see the same value

    – Jean-François Fabre
    Jan 1 at 22:59






  • 1





    Because in C, no two different objects have the same address. Pointers are objects, too. They are not the same as the thing they point at, just like an envelope is different from the house whose addres is printed on it.

    – rici
    Jan 1 at 23:01






  • 1





    Remember a pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. Where you normally think of a variable holding an immediate values, such as int a = 5;, a pointer would simply hold the address where 5 is stored in memory, e.g. int *b = &a;. If you need the value stored at the memory address b points to, you dereference b using the unary '*' operator, e.g. int c = *b; will initialize c = 5).

    – David C. Rankin
    Jan 1 at 23:13











  • @rici: Unlike most other programming languages, pointers in C are actual memory addresses, not objects. They're neither the envelope nor the house.

    – Robert Harvey
    Jan 1 at 23:15






  • 1





    @RobertHarvey -- pointers are objects in the sense that a pointer is a location in memory that holds some value. That is what C objects are.

    – David Bowling
    Jan 1 at 23:47
















3















Here is what I'm trying:



int a,b;
int *ptr;

a = 123;
ptr = &a;
b = *ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("&ptr is %pn",&ptr);
printf("&b is %pn",&b);


Results in:



&a is 0x7ffee01fc828
ptr points to 0x7ffee01fc828
&ptr is 0x7ffee01fc818
&b is 0x7ffee01fc820


I expected &b to show the same address as &a... But that's not the case, so I tried:



int a,*b;
int *ptr;

a = 123;
ptr = &a;
b = ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("&ptr is %pn",&ptr);
printf("&b is %pn",&b);


This still results in an unexpected memory address for &b:



&a is 0x7ffee1cbd828
ptr points to 0x7ffee1cbd828
&ptr is 0x7ffee1cbd818
&b is 0x7ffee1cbd820


Can someone help me understand why I'm unable to get &b matching the same address as &ptr or &a?



Thanks!










share|improve this question


















  • 1





    you're comparing pointers to pointers... drop the & you'll see the same value

    – Jean-François Fabre
    Jan 1 at 22:59






  • 1





    Because in C, no two different objects have the same address. Pointers are objects, too. They are not the same as the thing they point at, just like an envelope is different from the house whose addres is printed on it.

    – rici
    Jan 1 at 23:01






  • 1





    Remember a pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. Where you normally think of a variable holding an immediate values, such as int a = 5;, a pointer would simply hold the address where 5 is stored in memory, e.g. int *b = &a;. If you need the value stored at the memory address b points to, you dereference b using the unary '*' operator, e.g. int c = *b; will initialize c = 5).

    – David C. Rankin
    Jan 1 at 23:13











  • @rici: Unlike most other programming languages, pointers in C are actual memory addresses, not objects. They're neither the envelope nor the house.

    – Robert Harvey
    Jan 1 at 23:15






  • 1





    @RobertHarvey -- pointers are objects in the sense that a pointer is a location in memory that holds some value. That is what C objects are.

    – David Bowling
    Jan 1 at 23:47














3












3








3








Here is what I'm trying:



int a,b;
int *ptr;

a = 123;
ptr = &a;
b = *ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("&ptr is %pn",&ptr);
printf("&b is %pn",&b);


Results in:



&a is 0x7ffee01fc828
ptr points to 0x7ffee01fc828
&ptr is 0x7ffee01fc818
&b is 0x7ffee01fc820


I expected &b to show the same address as &a... But that's not the case, so I tried:



int a,*b;
int *ptr;

a = 123;
ptr = &a;
b = ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("&ptr is %pn",&ptr);
printf("&b is %pn",&b);


This still results in an unexpected memory address for &b:



&a is 0x7ffee1cbd828
ptr points to 0x7ffee1cbd828
&ptr is 0x7ffee1cbd818
&b is 0x7ffee1cbd820


Can someone help me understand why I'm unable to get &b matching the same address as &ptr or &a?



Thanks!










share|improve this question














Here is what I'm trying:



int a,b;
int *ptr;

a = 123;
ptr = &a;
b = *ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("&ptr is %pn",&ptr);
printf("&b is %pn",&b);


Results in:



&a is 0x7ffee01fc828
ptr points to 0x7ffee01fc828
&ptr is 0x7ffee01fc818
&b is 0x7ffee01fc820


I expected &b to show the same address as &a... But that's not the case, so I tried:



int a,*b;
int *ptr;

a = 123;
ptr = &a;
b = ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("&ptr is %pn",&ptr);
printf("&b is %pn",&b);


This still results in an unexpected memory address for &b:



&a is 0x7ffee1cbd828
ptr points to 0x7ffee1cbd828
&ptr is 0x7ffee1cbd818
&b is 0x7ffee1cbd820


Can someone help me understand why I'm unable to get &b matching the same address as &ptr or &a?



Thanks!







c pointers memory-address






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 22:57









Nathan PierceNathan Pierce

736




736








  • 1





    you're comparing pointers to pointers... drop the & you'll see the same value

    – Jean-François Fabre
    Jan 1 at 22:59






  • 1





    Because in C, no two different objects have the same address. Pointers are objects, too. They are not the same as the thing they point at, just like an envelope is different from the house whose addres is printed on it.

    – rici
    Jan 1 at 23:01






  • 1





    Remember a pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. Where you normally think of a variable holding an immediate values, such as int a = 5;, a pointer would simply hold the address where 5 is stored in memory, e.g. int *b = &a;. If you need the value stored at the memory address b points to, you dereference b using the unary '*' operator, e.g. int c = *b; will initialize c = 5).

    – David C. Rankin
    Jan 1 at 23:13











  • @rici: Unlike most other programming languages, pointers in C are actual memory addresses, not objects. They're neither the envelope nor the house.

    – Robert Harvey
    Jan 1 at 23:15






  • 1





    @RobertHarvey -- pointers are objects in the sense that a pointer is a location in memory that holds some value. That is what C objects are.

    – David Bowling
    Jan 1 at 23:47














  • 1





    you're comparing pointers to pointers... drop the & you'll see the same value

    – Jean-François Fabre
    Jan 1 at 22:59






  • 1





    Because in C, no two different objects have the same address. Pointers are objects, too. They are not the same as the thing they point at, just like an envelope is different from the house whose addres is printed on it.

    – rici
    Jan 1 at 23:01






  • 1





    Remember a pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. Where you normally think of a variable holding an immediate values, such as int a = 5;, a pointer would simply hold the address where 5 is stored in memory, e.g. int *b = &a;. If you need the value stored at the memory address b points to, you dereference b using the unary '*' operator, e.g. int c = *b; will initialize c = 5).

    – David C. Rankin
    Jan 1 at 23:13











  • @rici: Unlike most other programming languages, pointers in C are actual memory addresses, not objects. They're neither the envelope nor the house.

    – Robert Harvey
    Jan 1 at 23:15






  • 1





    @RobertHarvey -- pointers are objects in the sense that a pointer is a location in memory that holds some value. That is what C objects are.

    – David Bowling
    Jan 1 at 23:47








1




1





you're comparing pointers to pointers... drop the & you'll see the same value

– Jean-François Fabre
Jan 1 at 22:59





you're comparing pointers to pointers... drop the & you'll see the same value

– Jean-François Fabre
Jan 1 at 22:59




1




1





Because in C, no two different objects have the same address. Pointers are objects, too. They are not the same as the thing they point at, just like an envelope is different from the house whose addres is printed on it.

– rici
Jan 1 at 23:01





Because in C, no two different objects have the same address. Pointers are objects, too. They are not the same as the thing they point at, just like an envelope is different from the house whose addres is printed on it.

– rici
Jan 1 at 23:01




1




1





Remember a pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. Where you normally think of a variable holding an immediate values, such as int a = 5;, a pointer would simply hold the address where 5 is stored in memory, e.g. int *b = &a;. If you need the value stored at the memory address b points to, you dereference b using the unary '*' operator, e.g. int c = *b; will initialize c = 5).

– David C. Rankin
Jan 1 at 23:13





Remember a pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. Where you normally think of a variable holding an immediate values, such as int a = 5;, a pointer would simply hold the address where 5 is stored in memory, e.g. int *b = &a;. If you need the value stored at the memory address b points to, you dereference b using the unary '*' operator, e.g. int c = *b; will initialize c = 5).

– David C. Rankin
Jan 1 at 23:13













@rici: Unlike most other programming languages, pointers in C are actual memory addresses, not objects. They're neither the envelope nor the house.

– Robert Harvey
Jan 1 at 23:15





@rici: Unlike most other programming languages, pointers in C are actual memory addresses, not objects. They're neither the envelope nor the house.

– Robert Harvey
Jan 1 at 23:15




1




1





@RobertHarvey -- pointers are objects in the sense that a pointer is a location in memory that holds some value. That is what C objects are.

– David Bowling
Jan 1 at 23:47





@RobertHarvey -- pointers are objects in the sense that a pointer is a location in memory that holds some value. That is what C objects are.

– David Bowling
Jan 1 at 23:47












2 Answers
2






active

oldest

votes


















3















I expected &b to show the same address as &a…




Having that expectation means, that you have a wrong mental model about how pointers work and what their semantics are in the C programming language. The key misconception seems to happen at these two lines of the original code fragment:



First you have




b = *ptr;



This line translates into *"copy the contents of the memory at address ptr into the variable b." The variable b never even gets into contact with the pointer itself. You could rewrite it perfectly fine to the same effect into



int tmp = *ptr;
b = tmp;


As a matter of fact, every modern C compiler out there will produce identical code for either case.



The second line where you have a misconception is this one




printf("&b is %pn",&b);



specifically the effects of taking the address of the variable b, i.e. the result of &b. b is a completely independent variable, with its very own address. This address cannot be changed! The only thing that can be changed about a variable is its value.



Pointers are variables, too, and yes, a pointer does have an address itself. The value of a pointer is the address it points to. But like every variable, you cannot change the address where the variable is stored and when you assign an address to a pointer, you are changing its value. Hence when in your second code snipped you assign b = ptr; you're copying the value of ptr to b. After that, both ptr and b point to the same address, but these two pointers are two independent copies of the same value, where each copy is placed at a different place in memory. Naturally taking the address of b yields something different as the address of ptr.






share|improve this answer
























  • Thank you for going into depth to explain the misunderstanding I had. The class I'm taking on linkedin learning is limited when they explain these things, so I'm figuring it out by trial, error, and stackoverflow!

    – Nathan Pierce
    Jan 2 at 0:11



















3














now a,b and ptr are 3 different variables (regardless of their type)



So their address is different. You'll have the same values by not taking the address of pointer variables.



int a,*b;
int *ptr;

a = 123;
ptr = &a;
b = ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("b is %pn",b);





share|improve this answer



















  • 1





    Thank you for clarifying! Really appreciate you taking the time and being succinct.

    – Nathan Pierce
    Jan 2 at 0:10











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999585%2fmy-pointer-addresses-are-wrong-no-matter-what-i-try%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3















I expected &b to show the same address as &a…




Having that expectation means, that you have a wrong mental model about how pointers work and what their semantics are in the C programming language. The key misconception seems to happen at these two lines of the original code fragment:



First you have




b = *ptr;



This line translates into *"copy the contents of the memory at address ptr into the variable b." The variable b never even gets into contact with the pointer itself. You could rewrite it perfectly fine to the same effect into



int tmp = *ptr;
b = tmp;


As a matter of fact, every modern C compiler out there will produce identical code for either case.



The second line where you have a misconception is this one




printf("&b is %pn",&b);



specifically the effects of taking the address of the variable b, i.e. the result of &b. b is a completely independent variable, with its very own address. This address cannot be changed! The only thing that can be changed about a variable is its value.



Pointers are variables, too, and yes, a pointer does have an address itself. The value of a pointer is the address it points to. But like every variable, you cannot change the address where the variable is stored and when you assign an address to a pointer, you are changing its value. Hence when in your second code snipped you assign b = ptr; you're copying the value of ptr to b. After that, both ptr and b point to the same address, but these two pointers are two independent copies of the same value, where each copy is placed at a different place in memory. Naturally taking the address of b yields something different as the address of ptr.






share|improve this answer
























  • Thank you for going into depth to explain the misunderstanding I had. The class I'm taking on linkedin learning is limited when they explain these things, so I'm figuring it out by trial, error, and stackoverflow!

    – Nathan Pierce
    Jan 2 at 0:11
















3















I expected &b to show the same address as &a…




Having that expectation means, that you have a wrong mental model about how pointers work and what their semantics are in the C programming language. The key misconception seems to happen at these two lines of the original code fragment:



First you have




b = *ptr;



This line translates into *"copy the contents of the memory at address ptr into the variable b." The variable b never even gets into contact with the pointer itself. You could rewrite it perfectly fine to the same effect into



int tmp = *ptr;
b = tmp;


As a matter of fact, every modern C compiler out there will produce identical code for either case.



The second line where you have a misconception is this one




printf("&b is %pn",&b);



specifically the effects of taking the address of the variable b, i.e. the result of &b. b is a completely independent variable, with its very own address. This address cannot be changed! The only thing that can be changed about a variable is its value.



Pointers are variables, too, and yes, a pointer does have an address itself. The value of a pointer is the address it points to. But like every variable, you cannot change the address where the variable is stored and when you assign an address to a pointer, you are changing its value. Hence when in your second code snipped you assign b = ptr; you're copying the value of ptr to b. After that, both ptr and b point to the same address, but these two pointers are two independent copies of the same value, where each copy is placed at a different place in memory. Naturally taking the address of b yields something different as the address of ptr.






share|improve this answer
























  • Thank you for going into depth to explain the misunderstanding I had. The class I'm taking on linkedin learning is limited when they explain these things, so I'm figuring it out by trial, error, and stackoverflow!

    – Nathan Pierce
    Jan 2 at 0:11














3












3








3








I expected &b to show the same address as &a…




Having that expectation means, that you have a wrong mental model about how pointers work and what their semantics are in the C programming language. The key misconception seems to happen at these two lines of the original code fragment:



First you have




b = *ptr;



This line translates into *"copy the contents of the memory at address ptr into the variable b." The variable b never even gets into contact with the pointer itself. You could rewrite it perfectly fine to the same effect into



int tmp = *ptr;
b = tmp;


As a matter of fact, every modern C compiler out there will produce identical code for either case.



The second line where you have a misconception is this one




printf("&b is %pn",&b);



specifically the effects of taking the address of the variable b, i.e. the result of &b. b is a completely independent variable, with its very own address. This address cannot be changed! The only thing that can be changed about a variable is its value.



Pointers are variables, too, and yes, a pointer does have an address itself. The value of a pointer is the address it points to. But like every variable, you cannot change the address where the variable is stored and when you assign an address to a pointer, you are changing its value. Hence when in your second code snipped you assign b = ptr; you're copying the value of ptr to b. After that, both ptr and b point to the same address, but these two pointers are two independent copies of the same value, where each copy is placed at a different place in memory. Naturally taking the address of b yields something different as the address of ptr.






share|improve this answer














I expected &b to show the same address as &a…




Having that expectation means, that you have a wrong mental model about how pointers work and what their semantics are in the C programming language. The key misconception seems to happen at these two lines of the original code fragment:



First you have




b = *ptr;



This line translates into *"copy the contents of the memory at address ptr into the variable b." The variable b never even gets into contact with the pointer itself. You could rewrite it perfectly fine to the same effect into



int tmp = *ptr;
b = tmp;


As a matter of fact, every modern C compiler out there will produce identical code for either case.



The second line where you have a misconception is this one




printf("&b is %pn",&b);



specifically the effects of taking the address of the variable b, i.e. the result of &b. b is a completely independent variable, with its very own address. This address cannot be changed! The only thing that can be changed about a variable is its value.



Pointers are variables, too, and yes, a pointer does have an address itself. The value of a pointer is the address it points to. But like every variable, you cannot change the address where the variable is stored and when you assign an address to a pointer, you are changing its value. Hence when in your second code snipped you assign b = ptr; you're copying the value of ptr to b. After that, both ptr and b point to the same address, but these two pointers are two independent copies of the same value, where each copy is placed at a different place in memory. Naturally taking the address of b yields something different as the address of ptr.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 23:24









datenwolfdatenwolf

133k10135238




133k10135238













  • Thank you for going into depth to explain the misunderstanding I had. The class I'm taking on linkedin learning is limited when they explain these things, so I'm figuring it out by trial, error, and stackoverflow!

    – Nathan Pierce
    Jan 2 at 0:11



















  • Thank you for going into depth to explain the misunderstanding I had. The class I'm taking on linkedin learning is limited when they explain these things, so I'm figuring it out by trial, error, and stackoverflow!

    – Nathan Pierce
    Jan 2 at 0:11

















Thank you for going into depth to explain the misunderstanding I had. The class I'm taking on linkedin learning is limited when they explain these things, so I'm figuring it out by trial, error, and stackoverflow!

– Nathan Pierce
Jan 2 at 0:11





Thank you for going into depth to explain the misunderstanding I had. The class I'm taking on linkedin learning is limited when they explain these things, so I'm figuring it out by trial, error, and stackoverflow!

– Nathan Pierce
Jan 2 at 0:11













3














now a,b and ptr are 3 different variables (regardless of their type)



So their address is different. You'll have the same values by not taking the address of pointer variables.



int a,*b;
int *ptr;

a = 123;
ptr = &a;
b = ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("b is %pn",b);





share|improve this answer



















  • 1





    Thank you for clarifying! Really appreciate you taking the time and being succinct.

    – Nathan Pierce
    Jan 2 at 0:10
















3














now a,b and ptr are 3 different variables (regardless of their type)



So their address is different. You'll have the same values by not taking the address of pointer variables.



int a,*b;
int *ptr;

a = 123;
ptr = &a;
b = ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("b is %pn",b);





share|improve this answer



















  • 1





    Thank you for clarifying! Really appreciate you taking the time and being succinct.

    – Nathan Pierce
    Jan 2 at 0:10














3












3








3







now a,b and ptr are 3 different variables (regardless of their type)



So their address is different. You'll have the same values by not taking the address of pointer variables.



int a,*b;
int *ptr;

a = 123;
ptr = &a;
b = ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("b is %pn",b);





share|improve this answer













now a,b and ptr are 3 different variables (regardless of their type)



So their address is different. You'll have the same values by not taking the address of pointer variables.



int a,*b;
int *ptr;

a = 123;
ptr = &a;
b = ptr;

printf("&a is %pn",&a);
printf("ptr points to %pn",ptr);
printf("b is %pn",b);






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 23:01









Jean-François FabreJean-François Fabre

106k957115




106k957115








  • 1





    Thank you for clarifying! Really appreciate you taking the time and being succinct.

    – Nathan Pierce
    Jan 2 at 0:10














  • 1





    Thank you for clarifying! Really appreciate you taking the time and being succinct.

    – Nathan Pierce
    Jan 2 at 0:10








1




1





Thank you for clarifying! Really appreciate you taking the time and being succinct.

– Nathan Pierce
Jan 2 at 0:10





Thank you for clarifying! Really appreciate you taking the time and being succinct.

– Nathan Pierce
Jan 2 at 0:10


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999585%2fmy-pointer-addresses-are-wrong-no-matter-what-i-try%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement