LinuxQuestions.org
Help answer threads with 0 replies.
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 09-13-2004, 03:33 PM   #1
vijeesh_ep
Member
 
Registered: Jul 2004
Location: India
Distribution: Redhat-8
Posts: 35

Rep: Reputation: 15
Question question about pointers


I am submitting a Job interview question here



" The following system uses 4 bytes to store an intiger.Predict the output of the
progrem

main()
{
int *p =10;
int *q = 18;
printf("%d",p-q);
}

"


The answey i am getting is 2.Is it true always?

How can I clearly answer that question?
 
Old 09-13-2004, 04:13 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Yes, since the constraints specify that the system uses 4 bytes for an int. It works on the same theory that stepping through an array with a pointer does. If you did:
Code:
int main(void)
{
  int arr[2], *p;

  p = arr+2;
  printf("%d", p-arr);

  return 0;
}
p would actually be 8 memory addresses higher than arr, but it still prints 2.
Or if you did printf("%p %p\n", &arr[0], &arr[1]); you'd see that the memory addresses are 4 bytes apart.

Last edited by itsme86; 09-13-2004 at 04:15 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
a question about array pointers in C veritas Programming 12 07-18-2004 07:31 PM
simple c++ question (pointers) true_atlantis Programming 1 02-05-2004 07:34 PM
Another C pointers question from a newbie chris.hicks Programming 6 11-11-2003 06:14 AM
question on pointers h/w Programming 1 10-26-2003 03:54 PM
question on pointers to functions h/w Programming 3 10-06-2003 05:51 PM

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

All times are GMT -5. The time now is 09:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration