LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2017, 10:36 PM   #1
kapate
LQ Newbie
 
Registered: Dec 2016
Posts: 5

Rep: Reputation: Disabled
What is the difference C pointers?


Please:
what is the difference between all commented values?
Code:
#include <stdio.h>

int main(void)
{
	int a = 5;
	int *aPtr = &a;
	printf("%p  \n", aPtr);//0x7ffde04a673c
	printf("%p  \n", &aPtr);//0x7ffde04a6740
	printf("%p  \n", *aPtr);//0x5
	printf("%d  \n", aPtr);//-531994820  
	printf("%d  \n", &aPtr);//531994816
	printf("%d  \n", *aPtr);//5
};
 
Old 11-05-2017, 12:53 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Try to compress it into three lines:

Code:
	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);

Last edited by NevemTeve; 11-05-2017 at 12:56 AM.
 
1 members found this post helpful.
Old 11-05-2017, 03:41 AM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
I think you've just done his/her homework.
 
Old 11-05-2017, 03:44 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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
 
1 members found this post helpful.
Old 11-05-2017, 02:09 PM   #5
kapate
LQ Newbie
 
Registered: Dec 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
NevemTeve, pan64 thank you.
 
Old 11-05-2017, 08:09 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
"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.
 
2 members found this post helpful.
Old 11-06-2017, 12:03 AM   #7
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
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.
 
Old 11-06-2017, 03:41 AM   #8
kapate
LQ Newbie
 
Registered: Dec 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you for trying to help me, it is just a java developer question. not homework.

Last edited by kapate; 11-06-2017 at 03:42 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Freeing pointers to pointers devnull10 Programming 24 07-26-2012 04:58 AM
LXer: Making a Difference; Selling a Difference LXer Syndicated Linux News 0 09-23-2010 05:00 AM
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 04:26 AM
need help with pointers qanopus Programming 8 02-03-2003 05:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration