473,804 Members | 3,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorry ! a typo in the code

Sorry I made a typo in the code
The correct one is like this :

#include <stdio.h>
int main(void)
{
int *buf1 = malloc(10*sizeo f(int));
int *buf2 = malloc(10*sizeo f(int));

printf(buf1 > buf2 ? "Downwards" : "Upwards");
return 0;
}

I'm posting thru Google thats why this posting is not in the same thread :-(
Sorry for that !
Nov 14 '05 #1
5 1886
Nitin Bhardwaj wrote:

Sorry I made a typo in the code
The correct one is like this :

#include <stdio.h>
int main(void)
{
int *buf1 = malloc(10*sizeo f(int));
int *buf2 = malloc(10*sizeo f(int));

printf(buf1 > buf2 ? "Downwards" : "Upwards");
return 0;
}


That's undefined behavior.
If you have (as you do here)
two objects, which are not members of the same aggregate object,
then the only comparisons which are allowed for their addresses,
are that of equality and inequality.
Greater Than and Less Than operations are not defined
for the addresses of two separate objects.

--
pete
Nov 14 '05 #2
ni************* @hotmail.com (Nitin Bhardwaj) writes:
[...]
I'm posting thru Google thats why this posting is not in the same thread :-(
Sorry for that !


<OT>
It's hard to believe that Google doesn't let you post a proper
followup. (You might have to wait for your original article to
show up first.)
</OT>

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
ni************* @hotmail.com (Nitin Bhardwaj) wrote in message news:<17******* *************** ***@posting.goo gle.com>...
Sorry I made a typo in the code
The correct one is like this :

#include <stdio.h>
int main(void)
{
int *buf1 = malloc(10*sizeo f(int));
int *buf2 = malloc(10*sizeo f(int));

printf(buf1 > buf2 ? "Downwards" : "Upwards");
return 0;
}


Is this the way that we should compare two pointers?. Should we not use
void* pointer for a compersion? buf1, buf2 are int* and "Downwards" : "Upwards"
are string. The OP "forces" no conversion. Is he making a implicit conversion.
Is that correct. I ask since no body pointed out this. Please clear my doubt.
Nov 14 '05 #4

On Wed, 4 Aug 2004 da***********@y ahoo.com wrote:
int *buf1 = malloc(10*sizeo f(int));
int *buf2 = malloc(10*sizeo f(int));

printf(buf1 > buf2 ? "Downwards" : "Upwards");
Is this the way that we should compare two pointers?. Should we not use
void* pointer for a comparision?
You can always compare two object pointers of like type which point to
(parts of) the same object. For example, two pointers to 'int' can be
compared with '>' if they both point into the same array. Or you can
compare two pointers to 'char', or two void pointers. (There are other
rules: for example, IIRC you can compare a void pointer to any other
object pointer; but that's not nearly as important as the basic rule.)
buf1, buf2 are int* and "Downwards" : "Upwards" are string.
Correct. Notice that the comparison of 'buf1' and 'buf2' is illegal,
because while they have the right types, they don't point to comparable
addresses --- they point to two different malloc'ed blocks of memory.
The OP "forces" no conversion. Is he making a implicit conversion.


No. Both values are of type 'pointer to int' already; no conversions
take place.

The expression evaluates 'buf1 > buf2' (which is undefined, but let's
pretend it returns 0 or 1 like a normal comparison), and then yields
either "Downwards" (an array[10] of char) or "Upwards" (an array[8] of
char). In either case, the array of char /decays/ to a pointer to char,
which is then passed to 'printf'. 'printf' takes the string and prints
it on the screen. No conversions happen except for the array-to-pointer
decay, and that's nothing special.

HTH,
-Arthur
Nov 14 '05 #5
ni************* @hotmail.com (Nitin Bhardwaj) wrote in message news:<17******* *************** ***@posting.goo gle.com>...
Sorry I made a typo in the code
The correct one is like this :

#include <stdio.h>
int main(void)
{
int *buf1 = malloc(10*sizeo f(int));
int *buf2 = malloc(10*sizeo f(int));

printf(buf1 > buf2 ? "Downwards" : "Upwards");
return 0;
}


OP is comparing the two pointers *buf1 and *buf1 but it has nothing
to do with string printing in printf. Thanks Arthur J. O'Dwyer for
pointing. I must be more carfull in reading.
Nov 14 '05 #6

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

Similar topics

0
1295
by: Gregor | last post by:
When I drop a script on the BuildApplication tool, I get an error. It looks like there's a typo in BuildApplication.py, but after fixing the typo, I still get the same error. I guess the tool has its own compiled version of the file or something...? The error is: ------- File "Plataea:MacPython-OS9 2.3:Mac:scripts:BuildApplication.py", line
1
1336
by: Tommy.Ryding | last post by:
In the embedding Python manual at Python.org an example is presented that uses the function: PyDict_GetAttrString. Is it possible that this is a typo? I have searched the source code and I can't find it, I have also used google and I only get 4 hits. In for example Programming Python another function with similar name is described: PyObject_GetAttrString. Could that be the one that fit into the example?
5
1903
by: Lorenzo Villari | last post by:
Ahem... I think I've found a typo on my copy of The C Programming Language Second Edition. On section 1.9 (page 32 on my version, the Italian one ) you do find: #include <stdio.h> #define MAXLINE 1000 /* lunghezza massima di una linea */ int getline(char line, int maxline); void copy(char to, char from);
1
1690
by: Vijay Kumar R Zanvar | last post by:
Hello clc, I'm not sure if somebody had already noticed this, but I found a typo in n869. The example in 6.7.5.2#8 has a missing semicolon in the following declaration: typedef int VLA // OK - block scope typedef VLA. --
7
1331
by: mateus | last post by:
print "hello world" I have a nested loop where the outer loop iterates over key value pairs of a dictionary and the inner loop iterates over a list each list of which is a mapped value from the dictionary def showReport(self): for dev, sessions in self.logger.items(): for tree in session: self.addTestItem(self, tree)
12
3195
by: Henry Jones | last post by:
I have a VS 2005 VB.NET project and would like to change the color of the textbox when the user hovers over it. In a Module I have the following routines: Public Sub Button_Hover(ByRef btnName As Button) btnName.BackColor = Color.BlanchedAlmond End Sub
7
4778
JodiPhillips
by: JodiPhillips | last post by:
Hi, My first post! I've basically taught myself Access and the little I know about VBA through reading these forums and a couple of books. I'm in the middle of a project at work - to put it mildly I've been thrown in at the deep end, in the past I've just done simple databases and that's been fine. Now, however I have to "roll out" various front ends to a single back end for initially 10 users (and the bosses have plans for this to go to...
19
1809
by: Zach Heath | last post by:
I'm just starting C and haven't done programming for a few years...could you guys please take a look at this? Thanks for your time! I have an input file that looks like: 1.5 2.5 Bob, Joe 4.5 5.5 Bob, Jolene The first time through the do while loop it works fine but the second
11
14674
by: prasanna2100 | last post by:
Hi All, I need to use the DB2 Bulk Load option for loading DB2 table where source is also a DB2 table . Kindly provide some suggestions. Thanks in Advance Prasanna
0
9705
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
9576
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
10567
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
10323
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
10074
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
9138
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...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5515
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...
2
3809
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.