Print whole address block of some variable [closed]What are the differences between a pointer variable and a reference variable in C++?Improve INSERT-per-second performance of SQLite?Correct format specifier to print pointer or address?When defining a variable in C for example, where is the memory address for that variable stored?Storage of variables and dereferencing themVariable addresses are not aligned when they are not printedPrinting pointer's address in CHow I can remove all odds bits in C?Return binary results with newline incrementsPrinting type-casted types in C

What is a plausible power source to indefinitely sustain a space station?

Chemistry Riddle

If I have the Armor of Shadows Eldritch Invocation do I know the Mage Armor spell?

Considerations when providing money to one child now, and the other later?

What does a black-and-white Puerto Rican flag signify?

Can't understand how static works exactly

What gave NASA the confidence for a translunar injection in Apollo 8?

How can I deal with someone that wants to kill something that isn't supposed to be killed?

Are stackless C++20 coroutines a problem?

How am I supposed to put out fires?

Were Moshe's sons Jewish?

Why did NASA use Imperial units?

Why is DC so, so, so Democratic?

Killing a star safely

Xcode 10.3 Installation

How can I show that the speed of light in vacuum is the same in all reference frames?

Are rockets faster than airplanes?

Can you find Airpod Case using Find my iPhone?

How could Barty Crouch Jr. have run out of Polyjuice Potion at the end of the Goblet of Fire movie?

Were the Apollo broadcasts recorded locally on the LM?

Short story where a flexible reality hardens to an unchanging one

Do I care if the housing market has gone up or down, if I'm moving from one house to another?

Found more old paper shares from broken up companies

Is it better to merge "often" or only after completion do a big merge of feature branches?



Print whole address block of some variable [closed]


What are the differences between a pointer variable and a reference variable in C++?Improve INSERT-per-second performance of SQLite?Correct format specifier to print pointer or address?When defining a variable in C for example, where is the memory address for that variable stored?Storage of variables and dereferencing themVariable addresses are not aligned when they are not printedPrinting pointer's address in CHow I can remove all odds bits in C?Return binary results with newline incrementsPrinting type-casted types in C






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have this code:



#include <stdio.h>

int main(void)
int a = 56;
printf("First part of address block:n");
printf("%p", &a);
printf("All parts of address block:n");
printf("%pn%pn%pn%p", &a, &a + 1, &a + 2, &a + 3);
return 0;



Output:



First part of address block: 
0x7fffd029ecec
All parts of address block:
0x7fffd029ecec
0x7fffd029ecf0
0x7fffd029ecf4
0x7fffd029ecf8


In my opinion, address block of int a looks like this:



| 0x7fffd029ece**c** | 0x7fffd029ece**d** | 0x7fffd029ece**e** | 0x7fffd029ece**f** |
| 0000 | 0000 | 0011 | 1000









share|improve this question















closed as unclear what you're asking by Lundin, Toby Speight, Abhishek Pandey, Tomtom, Mathieu Mar 27 at 7:47


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


















  • int commonly has a size of 4, so that output looks right to me.

    – Blaze
    Mar 26 at 14:30











  • You might want to read about "pointer arithmetics".

    – Gerhardh
    Mar 26 at 14:31











  • You must cast to void * to get the proper type for %p. Also, print the size of a (printf("my int is %zu bytesn", sizeof a);) to make sure.

    – unwind
    Mar 26 at 14:31






  • 3





    What is the question?

    – Lundin
    Mar 26 at 14:52

















0















I have this code:



#include <stdio.h>

int main(void)
int a = 56;
printf("First part of address block:n");
printf("%p", &a);
printf("All parts of address block:n");
printf("%pn%pn%pn%p", &a, &a + 1, &a + 2, &a + 3);
return 0;



Output:



First part of address block: 
0x7fffd029ecec
All parts of address block:
0x7fffd029ecec
0x7fffd029ecf0
0x7fffd029ecf4
0x7fffd029ecf8


In my opinion, address block of int a looks like this:



| 0x7fffd029ece**c** | 0x7fffd029ece**d** | 0x7fffd029ece**e** | 0x7fffd029ece**f** |
| 0000 | 0000 | 0011 | 1000









share|improve this question















closed as unclear what you're asking by Lundin, Toby Speight, Abhishek Pandey, Tomtom, Mathieu Mar 27 at 7:47


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


















  • int commonly has a size of 4, so that output looks right to me.

    – Blaze
    Mar 26 at 14:30











  • You might want to read about "pointer arithmetics".

    – Gerhardh
    Mar 26 at 14:31











  • You must cast to void * to get the proper type for %p. Also, print the size of a (printf("my int is %zu bytesn", sizeof a);) to make sure.

    – unwind
    Mar 26 at 14:31






  • 3





    What is the question?

    – Lundin
    Mar 26 at 14:52













0












0








0








I have this code:



#include <stdio.h>

int main(void)
int a = 56;
printf("First part of address block:n");
printf("%p", &a);
printf("All parts of address block:n");
printf("%pn%pn%pn%p", &a, &a + 1, &a + 2, &a + 3);
return 0;



Output:



First part of address block: 
0x7fffd029ecec
All parts of address block:
0x7fffd029ecec
0x7fffd029ecf0
0x7fffd029ecf4
0x7fffd029ecf8


In my opinion, address block of int a looks like this:



| 0x7fffd029ece**c** | 0x7fffd029ece**d** | 0x7fffd029ece**e** | 0x7fffd029ece**f** |
| 0000 | 0000 | 0011 | 1000









share|improve this question
















I have this code:



#include <stdio.h>

int main(void)
int a = 56;
printf("First part of address block:n");
printf("%p", &a);
printf("All parts of address block:n");
printf("%pn%pn%pn%p", &a, &a + 1, &a + 2, &a + 3);
return 0;



Output:



First part of address block: 
0x7fffd029ecec
All parts of address block:
0x7fffd029ecec
0x7fffd029ecf0
0x7fffd029ecf4
0x7fffd029ecf8


In my opinion, address block of int a looks like this:



| 0x7fffd029ece**c** | 0x7fffd029ece**d** | 0x7fffd029ece**e** | 0x7fffd029ece**f** |
| 0000 | 0000 | 0011 | 1000






c pointers hex memory-address






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 14:50









abelenky

48.8k21 gold badges87 silver badges137 bronze badges




48.8k21 gold badges87 silver badges137 bronze badges










asked Mar 26 at 14:28









John DoeJohn Doe

11 bronze badge




11 bronze badge




closed as unclear what you're asking by Lundin, Toby Speight, Abhishek Pandey, Tomtom, Mathieu Mar 27 at 7:47


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as unclear what you're asking by Lundin, Toby Speight, Abhishek Pandey, Tomtom, Mathieu Mar 27 at 7:47


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • int commonly has a size of 4, so that output looks right to me.

    – Blaze
    Mar 26 at 14:30











  • You might want to read about "pointer arithmetics".

    – Gerhardh
    Mar 26 at 14:31











  • You must cast to void * to get the proper type for %p. Also, print the size of a (printf("my int is %zu bytesn", sizeof a);) to make sure.

    – unwind
    Mar 26 at 14:31






  • 3





    What is the question?

    – Lundin
    Mar 26 at 14:52

















  • int commonly has a size of 4, so that output looks right to me.

    – Blaze
    Mar 26 at 14:30











  • You might want to read about "pointer arithmetics".

    – Gerhardh
    Mar 26 at 14:31











  • You must cast to void * to get the proper type for %p. Also, print the size of a (printf("my int is %zu bytesn", sizeof a);) to make sure.

    – unwind
    Mar 26 at 14:31






  • 3





    What is the question?

    – Lundin
    Mar 26 at 14:52
















int commonly has a size of 4, so that output looks right to me.

– Blaze
Mar 26 at 14:30





int commonly has a size of 4, so that output looks right to me.

– Blaze
Mar 26 at 14:30













You might want to read about "pointer arithmetics".

– Gerhardh
Mar 26 at 14:31





You might want to read about "pointer arithmetics".

– Gerhardh
Mar 26 at 14:31













You must cast to void * to get the proper type for %p. Also, print the size of a (printf("my int is %zu bytesn", sizeof a);) to make sure.

– unwind
Mar 26 at 14:31





You must cast to void * to get the proper type for %p. Also, print the size of a (printf("my int is %zu bytesn", sizeof a);) to make sure.

– unwind
Mar 26 at 14:31




3




3





What is the question?

– Lundin
Mar 26 at 14:52





What is the question?

– Lundin
Mar 26 at 14:52












2 Answers
2






active

oldest

votes


















3














C increment pointers not by bytes, but by size of type is pointed on.
That is, if you have pointer to int it increments by sizeof(int) - in your example is 4.






share|improve this answer























  • When I have char a = 'A' and do the same thing as in my post: &a + 1, will it be +1? Am I right?

    – John Doe
    Mar 26 at 14:43











  • @JohnDoe Yes, since sizeof(char) is always 1

    – Spikatrix
    Mar 26 at 14:48


















0














As Alex said, pointer math increments by the size of the type, not the byte.



You can make it meet your expectation by casting &a to a char* (aka. byte-pointer):



int main(void) 
int a = 56;
printf("First part of address block:n");
printf("%p", &a);
printf("All parts of address block:n");
printf("%pn%pn%pn%p", &a, ((char*)&a) + 1, ((char*)&a) + 2, ((char*)&a) + 3);
return 0;



Here is how I'd write it to make it handle the platform size:



#include <stdio.h>

int main(void)
int a = 56;
char* p = (char*)&a;
printf("First part of address block:n");
printf("%pn", &a);
printf("All parts of address block:n");
for(int i=0;i<sizeof(a); ++i) printf("%p : [%d] : 0x%02xn", p+i, i, *(p+i));
return 0;



Output



Success #stdin #stdout 0s 9424KB
First part of address block:
0x7ffff2e39c5c
All parts of address block:
0x7ffff2e39c5c : [0] : 0x38
0x7ffff2e39c5d : [1] : 0x00
0x7ffff2e39c5e : [2] : 0x00
0x7ffff2e39c5f : [3] : 0x00





share|improve this answer























  • Can you give me code how to print also value? If first part is 0000, value will be 0 ... last block is 1000, value will be 8. I tried printf("Value: %d", *((char*)&a)), but it does not work.

    – John Doe
    Mar 26 at 15:02




















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














C increment pointers not by bytes, but by size of type is pointed on.
That is, if you have pointer to int it increments by sizeof(int) - in your example is 4.






share|improve this answer























  • When I have char a = 'A' and do the same thing as in my post: &a + 1, will it be +1? Am I right?

    – John Doe
    Mar 26 at 14:43











  • @JohnDoe Yes, since sizeof(char) is always 1

    – Spikatrix
    Mar 26 at 14:48















3














C increment pointers not by bytes, but by size of type is pointed on.
That is, if you have pointer to int it increments by sizeof(int) - in your example is 4.






share|improve this answer























  • When I have char a = 'A' and do the same thing as in my post: &a + 1, will it be +1? Am I right?

    – John Doe
    Mar 26 at 14:43











  • @JohnDoe Yes, since sizeof(char) is always 1

    – Spikatrix
    Mar 26 at 14:48













3












3








3







C increment pointers not by bytes, but by size of type is pointed on.
That is, if you have pointer to int it increments by sizeof(int) - in your example is 4.






share|improve this answer













C increment pointers not by bytes, but by size of type is pointed on.
That is, if you have pointer to int it increments by sizeof(int) - in your example is 4.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 26 at 14:33









AlexAlex

3962 silver badges13 bronze badges




3962 silver badges13 bronze badges












  • When I have char a = 'A' and do the same thing as in my post: &a + 1, will it be +1? Am I right?

    – John Doe
    Mar 26 at 14:43











  • @JohnDoe Yes, since sizeof(char) is always 1

    – Spikatrix
    Mar 26 at 14:48

















  • When I have char a = 'A' and do the same thing as in my post: &a + 1, will it be +1? Am I right?

    – John Doe
    Mar 26 at 14:43











  • @JohnDoe Yes, since sizeof(char) is always 1

    – Spikatrix
    Mar 26 at 14:48
















When I have char a = 'A' and do the same thing as in my post: &a + 1, will it be +1? Am I right?

– John Doe
Mar 26 at 14:43





When I have char a = 'A' and do the same thing as in my post: &a + 1, will it be +1? Am I right?

– John Doe
Mar 26 at 14:43













@JohnDoe Yes, since sizeof(char) is always 1

– Spikatrix
Mar 26 at 14:48





@JohnDoe Yes, since sizeof(char) is always 1

– Spikatrix
Mar 26 at 14:48













0














As Alex said, pointer math increments by the size of the type, not the byte.



You can make it meet your expectation by casting &a to a char* (aka. byte-pointer):



int main(void) 
int a = 56;
printf("First part of address block:n");
printf("%p", &a);
printf("All parts of address block:n");
printf("%pn%pn%pn%p", &a, ((char*)&a) + 1, ((char*)&a) + 2, ((char*)&a) + 3);
return 0;



Here is how I'd write it to make it handle the platform size:



#include <stdio.h>

int main(void)
int a = 56;
char* p = (char*)&a;
printf("First part of address block:n");
printf("%pn", &a);
printf("All parts of address block:n");
for(int i=0;i<sizeof(a); ++i) printf("%p : [%d] : 0x%02xn", p+i, i, *(p+i));
return 0;



Output



Success #stdin #stdout 0s 9424KB
First part of address block:
0x7ffff2e39c5c
All parts of address block:
0x7ffff2e39c5c : [0] : 0x38
0x7ffff2e39c5d : [1] : 0x00
0x7ffff2e39c5e : [2] : 0x00
0x7ffff2e39c5f : [3] : 0x00





share|improve this answer























  • Can you give me code how to print also value? If first part is 0000, value will be 0 ... last block is 1000, value will be 8. I tried printf("Value: %d", *((char*)&a)), but it does not work.

    – John Doe
    Mar 26 at 15:02
















0














As Alex said, pointer math increments by the size of the type, not the byte.



You can make it meet your expectation by casting &a to a char* (aka. byte-pointer):



int main(void) 
int a = 56;
printf("First part of address block:n");
printf("%p", &a);
printf("All parts of address block:n");
printf("%pn%pn%pn%p", &a, ((char*)&a) + 1, ((char*)&a) + 2, ((char*)&a) + 3);
return 0;



Here is how I'd write it to make it handle the platform size:



#include <stdio.h>

int main(void)
int a = 56;
char* p = (char*)&a;
printf("First part of address block:n");
printf("%pn", &a);
printf("All parts of address block:n");
for(int i=0;i<sizeof(a); ++i) printf("%p : [%d] : 0x%02xn", p+i, i, *(p+i));
return 0;



Output



Success #stdin #stdout 0s 9424KB
First part of address block:
0x7ffff2e39c5c
All parts of address block:
0x7ffff2e39c5c : [0] : 0x38
0x7ffff2e39c5d : [1] : 0x00
0x7ffff2e39c5e : [2] : 0x00
0x7ffff2e39c5f : [3] : 0x00





share|improve this answer























  • Can you give me code how to print also value? If first part is 0000, value will be 0 ... last block is 1000, value will be 8. I tried printf("Value: %d", *((char*)&a)), but it does not work.

    – John Doe
    Mar 26 at 15:02














0












0








0







As Alex said, pointer math increments by the size of the type, not the byte.



You can make it meet your expectation by casting &a to a char* (aka. byte-pointer):



int main(void) 
int a = 56;
printf("First part of address block:n");
printf("%p", &a);
printf("All parts of address block:n");
printf("%pn%pn%pn%p", &a, ((char*)&a) + 1, ((char*)&a) + 2, ((char*)&a) + 3);
return 0;



Here is how I'd write it to make it handle the platform size:



#include <stdio.h>

int main(void)
int a = 56;
char* p = (char*)&a;
printf("First part of address block:n");
printf("%pn", &a);
printf("All parts of address block:n");
for(int i=0;i<sizeof(a); ++i) printf("%p : [%d] : 0x%02xn", p+i, i, *(p+i));
return 0;



Output



Success #stdin #stdout 0s 9424KB
First part of address block:
0x7ffff2e39c5c
All parts of address block:
0x7ffff2e39c5c : [0] : 0x38
0x7ffff2e39c5d : [1] : 0x00
0x7ffff2e39c5e : [2] : 0x00
0x7ffff2e39c5f : [3] : 0x00





share|improve this answer













As Alex said, pointer math increments by the size of the type, not the byte.



You can make it meet your expectation by casting &a to a char* (aka. byte-pointer):



int main(void) 
int a = 56;
printf("First part of address block:n");
printf("%p", &a);
printf("All parts of address block:n");
printf("%pn%pn%pn%p", &a, ((char*)&a) + 1, ((char*)&a) + 2, ((char*)&a) + 3);
return 0;



Here is how I'd write it to make it handle the platform size:



#include <stdio.h>

int main(void)
int a = 56;
char* p = (char*)&a;
printf("First part of address block:n");
printf("%pn", &a);
printf("All parts of address block:n");
for(int i=0;i<sizeof(a); ++i) printf("%p : [%d] : 0x%02xn", p+i, i, *(p+i));
return 0;



Output



Success #stdin #stdout 0s 9424KB
First part of address block:
0x7ffff2e39c5c
All parts of address block:
0x7ffff2e39c5c : [0] : 0x38
0x7ffff2e39c5d : [1] : 0x00
0x7ffff2e39c5e : [2] : 0x00
0x7ffff2e39c5f : [3] : 0x00






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 26 at 14:42









abelenkyabelenky

48.8k21 gold badges87 silver badges137 bronze badges




48.8k21 gold badges87 silver badges137 bronze badges












  • Can you give me code how to print also value? If first part is 0000, value will be 0 ... last block is 1000, value will be 8. I tried printf("Value: %d", *((char*)&a)), but it does not work.

    – John Doe
    Mar 26 at 15:02


















  • Can you give me code how to print also value? If first part is 0000, value will be 0 ... last block is 1000, value will be 8. I tried printf("Value: %d", *((char*)&a)), but it does not work.

    – John Doe
    Mar 26 at 15:02

















Can you give me code how to print also value? If first part is 0000, value will be 0 ... last block is 1000, value will be 8. I tried printf("Value: %d", *((char*)&a)), but it does not work.

– John Doe
Mar 26 at 15:02






Can you give me code how to print also value? If first part is 0000, value will be 0 ... last block is 1000, value will be 8. I tried printf("Value: %d", *((char*)&a)), but it does not work.

– John Doe
Mar 26 at 15:02




Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript