Sorry ! a typo in the code

N

Nitin Bhardwaj

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 !
 
P

pete

Nitin said:
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.
 
K

Keith Thompson

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>
 
D

dam_fool_2003

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.
 
A

Arthur J. O'Dwyer

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
 
D

dam_fool_2003

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top