473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what will happen if we print the following...

int *s=(int *)2000;
int *p=(int *)1000;

printf("%d",p-s);

May 25 '07 #1
10 1715
On 25 Maj, 14:47, Shraddha <shraddhajosh.. .@gmail.comwrot e:
int *s=(int *)2000;
int *p=(int *)1000;

printf("%d",p-s);
What do you think will happen?

By the way, this is probably better asked in comp.lang.c

--
Erik Wikström

May 25 '07 #2
Shraddha wrote:
int *s=(int *)2000;
int *p=(int *)1000;

printf("%d",p-s);
This question doesn't make any sense. You'll take the time to post this
to a newsgroup, but you won't compile the code and see for yourself what
happens?

May 25 '07 #3
On May 25, 8:47 am, Shraddha <shraddhajosh.. .@gmail.comwrot e:
int *s=(int *)2000;
int *p=(int *)1000;

printf("%d",p-s);

What will happen is nowhere near as important as what the intention of
the code is. What are you trying to do?

May 25 '07 #4
"Salt_Peter " writes:
On May 25, 8:47 am, Shraddha <shraddhajosh.. .@gmail.comwrot e:
>int *s=(int *)2000;
int *p=(int *)1000;

printf("%d", p-s);


What will happen is nowhere near as important as what the intention of
the code is. What are you trying to do?
It looks to me like he is trying to learn the rules of some computer
language. And perhaps its interaction with its native habitat.
May 25 '07 #5
osmium wrote:
"Salt_Peter " writes:
On May 25, 8:47 am, Shraddha <shraddhajosh.. .@gmail.comwrot e:
int *s=(int *)2000;
int *p=(int *)1000;
>
printf("%d",p-s);

What will happen is nowhere near as important as what the intention
of the code is. What are you trying to do?

It looks to me like he is trying to learn the rules of some computer
language. And perhaps its interaction with its native habitat.
Learning a programming language, especially a large one like C++, by
random usenet posts is a fool's game. It wastes everyone's time.

He needs to get a decent book and work through it, asking questions
about things he can't figure out.


Brian
May 25 '07 #6
On May 25, 2:47 pm, Shraddha <shraddhajosh.. .@gmail.comwrot e:
int *s=(int *)2000;
int *p=(int *)1000;
printf("%d",p-s);
It depends very much on the implementation, and it could core
dump, or crash the system. On most systems, however, it will
probably output the value of 1000/sizeof(int).

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 25 '07 #7
On May 25, 3:07 pm, "osmium" <r124c4u...@com cast.netwrote:
"Salt_Peter " writes:
On May 25, 8:47 am, Shraddha <shraddhajosh.. .@gmail.comwrot e:
int *s=(int *)2000;
int *p=(int *)1000;
printf("%d",p-s);
What will happen is nowhere near as important as what the intention of
the code is. What are you trying to do?

It looks to me like he is trying to learn the rules of some computer
language. And perhaps its interaction with its native habitat.
Unfortunately, that appears to be the case (although i'ld replace
'rules' with 'effects'). I'ld suggest that setting pointers to
unallocated memory using magic numbers is begging for a whiplashing.
Calculating the offset between 2 memory locations and dividing a
hypothetical sizeof(int) is not a constructive discussion for the OP.
Thats not what C++ is about, IMHO.

He might as well do:
#include <iostream>

int main()
{
size_t t(1000);
std::cout << t/sizeof(int) << std::endl;
}

I'ld rather he come forth and explain the purpose or expectations of
the code before we start hitting him with semantics (prefer
static_cast<>() to old casts, magic pointers are evil, etc).
May 25 '07 #8
"Shraddha" wrote:
int *s=(int *)2000;
int *p=(int *)1000;

printf("%d",p-s);
You would do well to compile this, run it, see what happens and remeber it.
#include <stdio.h>

int main(void)
{
double x = 1.e35;
int n;
n = (int) x;
printf("%d\n", n);
return 0;
}

The message is clear. A cast says: "compiler, do this conversion. I am a
smart human and I know what I am doing." The compiler believes you and does
its best to comply. The results may be nonsense, which is what I get on my
compiler. It prints -250. The compilers I have used say "can not convert'
in the pre-cast error message. It really means: I *will not*, unless you
explicitly tell me to.
May 25 '07 #9
"osmium" writes:

#include <stdio.h>

int main(void)
{
double x = 1.e35;
int n;
n = (int) x;
printf("%d\n", n);
return 0;
}
Oops. That's C code, I read it as a C problem and answered accordingly.
May 25 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
19061
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
33
3847
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body - regardless of the loop beeing entered or not. So where is an actual use case for that feature? I imagined that the else-clause would only be executed if the loop body
121
10107
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
25
3091
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I don't have a copy of ANSI C89 standard,therefore i had to post this question: What is the difference between "unspecified" behaviour & "undefined" behaviour of some C Code ??
6
2649
by: Niklaus | last post by:
Hi, Can someone point out what is wrong with this code ? How can i make it better optimize it. When run it gives me seg fault in linux. But windows it works fine(runs for a long time). Do we have something like stack size growing enormously and then saying you can't access ,so a segfault ? It would be helpful if someone can run the code and give me the output. It takes a long time on my PC.
11
14821
by: antonyliu2002 | last post by:
I know that this has been asked and answered thousands of times. As a matter of fact, I know that I need to say If Not Page.IsPostBack Then 'Do something End If for things that needs to be processed by the web server. I am still struggling to understand this postback logic, and hope that some kind gurus out there could help me clarify the confusion I have.
7
1712
by: Yvonne | last post by:
We have a Contacts & Events database made up of three tables viz tblContacts, tblEvents and tblAttendance with the latter linking contacts to particular events. The tblContacts table has a Yes/No field called 'Select". When we want to view the Contacts for a particular event, we do so in a continuous form driven by a query. What we want to do is have a button that will update all the 'Select'
4
306
by: Shraddha | last post by:
for( ; ; ); printf("Hi");
8
1145
by: Brian Munroe | last post by:
My example: class A(object): def __init__(self, name): self.__name = name def getName(self): return self.__name
0
8801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9174
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.