Exforsys

Online Training

Confused with Result

This is a discussion on Confused with Result within the C and C++ forums, part of the Programming Talk category; I have my C program as below: void main() { int *s,*t; s=(int*)2000; t=(int*)3000; printf("&#...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-05-2007, 04:01 AM
Senior Member
 
Join Date: Apr 2006
Posts: 122
rachelle is on a distinguished road
Exclamation Confused with Result

I have my C program as below:

void main()
{
int *s,*t;
s=(int*)2000;
t=(int*)3000;
printf("%d",(t-s));
}

I thought the output would be 1000. But the output was 500.I was confused with why I got this output. Someone explain me on this.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-06-2007, 04:39 AM
Junior Member
 
Join Date: May 2007
Posts: 12
Mukhtar Ahmad is on a distinguished road
t and s are pointer variables not int and they hold memory address
try using

printf("%d",(*t-*s)); instead of printf("%d",(t-s));
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-06-2007, 07:42 AM
Senior Member
 
Join Date: Apr 2006
Posts: 141
Adrian is on a distinguished road
The result 500 is perfectly right. This is because t and s are declared as integer pointer variable and integer variables occupy 2 bytes generally and so the difference of 1000 space is equal to 500 integer spaces apart. This is the reason for getting output as 500.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-08-2007, 06:47 PM
Junior Member
 
Join Date: Jun 2007
Posts: 3
tarungosain20 is on a distinguished road
because size of integer is 2 bytes.
the statement subtracts 3000-2000 in steps of 2 .
hence the answer is 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
expected and actual result anjaligupta_28 Software Testing 5 09-12-2007 12:08 PM
Result of Program ashlee C and C++ 1 05-10-2007 09:22 AM
query result b_s_meena SQL Server 2005 Tutorials 4 02-16-2007 03:44 AM
techmahindra fresher result yash_mave Career Advice 0 06-01-2006 08:14 AM
TechMahindra Fresher Drive Result yash_mave Suggestions & Feedback 0 05-27-2006 06:48 AM


All times are GMT -4. The time now is 12:37 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.