ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
printf("value of the pointer: %p (%ld)\n", aPtr, (long)aPtr);
printf("address of the pointer: %p (%ld)\n", &aPtr, (long)&aPtr);
printf("points to the value: %p (%ld)\n", (void *)*aPtr, (long)*aPtr);
also try these, and compare the results:
Code:
printf("value of 'a': %p (%ld)\n", (void *)a, (long)a);
printf("address of 'a': %p (%ld)\n", &a, (long)&a);
aPtr is a variable, actually used as a pointer to somewhere
Code:
printf("%p \n", aPtr);//0x7ffde04a673c // this is the value stored in variable aPtr, contains the address of the variable a
printf("%p \n", &aPtr);//0x7ffde04a6740 // this is the address of variable aPtr
printf("%p \n", *aPtr);//0x5 // this is the <something> found at the address where aPtr points)
printf("%d \n", aPtr);//-531994820 // using %d is more or less meaningless on a 64bit system
printf("%d \n", &aPtr);//531994816 // using %d is more or less meaningless
printf("%d \n", *aPtr);//5 // but will work if you want to print a one-digit number
by the way, 0xe04a6740 is equal to 3762972480 and 3762972480+531994816 = 4294967296 which is equal to 0x100000000
"A pointer" is "an ordinary integer-type variable" whose value is understood to represent an address. (By convention, "zero" means NULL.)
Thus, in the example program, we have an integer variable named a, and, right next to it, another variable named aPtr. We can see from the program output that, on the target machine, both of these are 4 bytes long. The only difference between the two is the manner in which they are used.
What I usually do – still(!) do – is to grab a piece of paper and a number-two pencil(!), and draw a picture.
The only difference between the two is the manner in which they are used.
I wonder if it could fit in the scope of the current homework assignment, if we cast some of those ... values .., around a bit. If the pupil grasps the concept, she/he will be less surprised in the future and get a free ticket to the fifth dimension, once that some more interesting pointers pop up.
Edit: ... yeah. And I just confirmed that I am too afraid to express it all in the English language, myself. Never mind.
Last edited by Michael Uplawski; 11-06-2017 at 12:05 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.