LinuxQuestions.org
Visit Jeremy's Blog.
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 07-18-2009, 09:01 AM   #1
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Rep: Reputation: 15
Thumbs up Program on pointers- explanation needed


Hi

Code:
   main ()
   {
      int a[] = {10,60,30,40,50};
      char *p;
      p = (char *)a;
      printf("%d",*((int *)(p+4)));
   }
output is 60

Code:
   main ()
   {
      int a[] = {10,60,30,40,50};
      char *p;
      p = (char *)a;
      printf("%d",*((int *)p+4));
   }
output is 50

Please explain how both outputs are obtained.

Regards
thelink
 
Old 07-18-2009, 09:48 AM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
This smells like homework, so Ill wont say exactly what the awnser is.

Ill assume you can figure out where the intresting part of the program is, so Ill just say that the key is in the order of operations and the effects they have on the datatype the compiler sees (importent because diffrent types have diffrent sizes).
 
Old 07-18-2009, 11:47 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by thelink123 View Post
Hi

Code:
   main ()
   {
      int a[] = {10,60,30,40,50};
      char *p;
      p = (char *)a;
      printf("%d",*((int *)(p+4)));
   }
output is 60

Code:
   main ()
   {
      int a[] = {10,60,30,40,50};
      char *p;
      p = (char *)a;
      printf("%d",*((int *)p+4));
   }
output is 50

Please explain how both outputs are obtained.

Regards
thelink

You have to exactly understand how

Code:
pointer + offset
is calculated - it is not just an arithmetic sum of pointer, offset.

The answer is in any "C" textbook in the chapters on pointers.
 
Old 07-18-2009, 02:23 PM   #4
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
Hi

Like SciYro said, it has to do with order of operations. Without giving an exact answer, you have to determine the data type of p in each case before the pointer addition takes place.
In one case, p is still char* and in other case it's int* and that will affect the pointer addition (ie how many bytes you are moving).

Last edited by rkski; 07-18-2009 at 02:27 PM.
 
Old 07-18-2009, 02:39 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
the issue here isn't so much "pointers" as "precedence".

Here's another (completely different) example of (exactly the same) issue:
Quote:
int i = 3 + 5 * 2;
int j = (3 + 5) * 2;
printf ("i=%d, j=%d\n", i, j);
In other words,
Quote:
(int *)(p+4) != (int *)p+4
'Hope that helps .. PSM

Last edited by paulsm4; 07-18-2009 at 10:57 PM.
 
  


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
New to C++, some pointers are needed. ichigo@tea Programming 3 09-30-2008 01:28 PM
File structures registration in linux modules to the kernel - explanation needed Neuzen Linux - Kernel 0 09-23-2008 07:54 AM
A total explanation of Sharing and Accessing SuSE 9.2 in WIN Xp is needed. HELP ! ! ! bedi-beparwah Linux - Networking 1 03-31-2005 12:45 PM
broad explanation needed stabu Linux - Newbie 1 10-09-2004 06:22 AM
Some GNOME 2.6 pointers needed Rajahuroman Linux - Software 2 05-23-2004 02:07 PM

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

All times are GMT -5. The time now is 01:35 PM.

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