Sandeep, you have already been asked not to quote signatures.
In fact, the post to which you made this reply contained just such
a request: "Please don't quote signatures," it said. And yet, you
keep on quoting signatures. Is your reading comprehension a bit
below par, or are you just trying to annoy people?
It's interesting, some of the ones you label as OK are not OK on my
compiler but none of them generate any warning.
It should come as no surprise that there exist fragments of C
code that are individually unobjectionable but collectively wrong.
For example, there's nothing wrong with taking an integer variable
x and dividing it by an integer variable y to get a quotient:
int quotient(int x, int y) {
return x / y;
}
All plain and above-board, right? Now try `quotient(42, 0)'. So,
don't equate "No complaints from the compiler" with "My code is
correct," or even with "My code's behavior is well-defined."
I tried out this code:
#include<stdio.h>
#include<stdlib.h>
main()
{
char* c;
void* v;
int* i=malloc(4);
You're assuming that sizeof(int) <= 4. Although this is true
on many machines it is not true universally, and besides it's silly
to make unnecessary assumptions. Write malloc(sizeof(int)), or
even better write malloc(sizeof *i).
You're also assuming that malloc() cannot fail, which just shows
that you are still wet behind the ears.
Okay, you now have c pointing to the first char of the same
object i points to. This will be one of the chars that make up the
representation of the integer value 257 (assuming 4 bytes are in
fact sufficient).
Now you print the value of that char. What you get is entirely
up to the implementation, because different implementations use
different schemes to store int values. In the commonest schemes,
two bytes of the representation of 257 will be 1 and the others (if
there are any) will be 0 -- but even that's not guaranteed. You
report that on your system the printed value was 1, so we can guess
(only guess) that your system uses one of the straightforward schemes,
but we cannot conclude as you do that it "doesn't work."
Here, at least, you're requesting the right amount: sizeof(char)
is 1 by definition. But you're still assuming that malloc() doesn't
ever fail.
Okay, i now points to the spot where c points. But there's only
1 byte there (assuming malloc() succeeded), so if sizeof(int)>1 (which
the earlier printout proved is true for your system), you've got a
pointer-to-int aimed at memory-too-small-for-int.
... and here you try to read the entire int from the too-small
piece of memory. What happens is not just implementation-defined,
but undefined. *Any* output (or no output) is permitted, therefore
it is impossible to make deductions from whatever output you do or
don't see. It's possible to make guesses, but the guesses cannot
lead to a sure conclusion -- in particular, they cannot lead to the
conclusion "it succeeds."
}
It produces
1
7
So the "int to char via void" fails but "char to int via void" succeeds.
No; all you've shown is that you don't understand what you're
doing. I recommend more time with your C textbook or reference.
Fooling around just to see what happens is fine, to a point, but you
should at least be aware of when you're writing valid code and when
you're writing junk -- and you should know enough not to base any
deductions on the junk.
Of course, "Read the textbook" will only be helpful if your
reading comprehension improves above its present level. Please
don't quote signatures.