Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-13-2004, 02:33 PM   #1
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

int *p =10;
int *q = 18;


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

How can I clearly answer that question?
Old 09-13-2004, 03:13 PM   #2
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:
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 03:15 PM.


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 06:31 PM
simple c++ question (pointers) true_atlantis Programming 1 02-05-2004 06:34 PM
Another C pointers question from a newbie chris.hicks Programming 6 11-11-2003 05:14 AM
question on pointers h/w Programming 1 10-26-2003 02:54 PM
question on pointers to functions h/w Programming 3 10-06-2003 04:51 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:29 PM.

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