By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,027 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,027 IT Pros & Developers. It's quick & easy.

Sorry ! a typo in the code

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

#include <stdio.h>
int main(void)
{
int *buf1 = malloc(10*sizeof(int));
int *buf2 = malloc(10*sizeof(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
Share this Question
Share on Google+
5 Replies


P: n/a
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*sizeof(int));
int *buf2 = malloc(10*sizeof(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

P: n/a
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_Keith) 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

P: n/a
ni*************@hotmail.com (Nitin Bhardwaj) wrote in message news:<17*************************@posting.google.c om>...
Sorry I made a typo in the code
The correct one is like this :

#include <stdio.h>
int main(void)
{
int *buf1 = malloc(10*sizeof(int));
int *buf2 = malloc(10*sizeof(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

P: n/a

On Wed, 4 Aug 2004 da***********@yahoo.com wrote:
int *buf1 = malloc(10*sizeof(int));
int *buf2 = malloc(10*sizeof(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

P: n/a
ni*************@hotmail.com (Nitin Bhardwaj) wrote in message news:<17*************************@posting.google.c om>...
Sorry I made a typo in the code
The correct one is like this :

#include <stdio.h>
int main(void)
{
int *buf1 = malloc(10*sizeof(int));
int *buf2 = malloc(10*sizeof(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 discussion thread is closed

Replies have been disabled for this discussion.