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

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?