0
struct node *tempNode = (struct node*) malloc(sizeof(struct node));

//and

struct node *tempNode = malloc(sizeof(struct node));
Ultimater
  • 4,647
  • 2
  • 29
  • 43
  • `(struct node*)` silences compiler errors as malloc returns a void pointer. – Ultimater Nov 22 '16 at 18:52
  • 1
    @Ultimater In C there won't be any warning. See http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Eugene Sh. Nov 22 '16 at 18:52
  • 2
    The first is considered bad practce in C; you haven't needed to explicitly cast the return value of `malloc` since the C90 standard, and under that standard doing so could suppress a useful diagnostic. And an even better way to write it is `struct node *tempNode = malloc( sizeof *tempNode );`. – John Bode Nov 22 '16 at 18:52
  • The question title here should be reformulated as *Do I cast the result of malloc*, shouldn't it? But then it is apparent that it is a duplicate and should be closed as such, I think. – Jens Gustedt Nov 22 '16 at 20:25

4 Answers4

0
struct node *tempNode = (struct node*) malloc(sizeof(struct node));

you're dynamically allocating space to a node pointer tempNode and typecasting it to be a struct node* type. (pointer to structure)

struct node *tempNode = malloc(sizeof(struct node));

The same as above, except without the explicit typecast (struct node*)

baconSoda
  • 503
  • 2
  • 5
  • 15
0

There is no difference. Since malloc returns type void *, it can be assigned to a variable of any pointer type without casting. As John Bode mentioned, casting the result of malloc is considered bad practice because it may mask the compiler error that would result from having some other definition of malloc.

Andy Schweig
  • 6,597
  • 2
  • 16
  • 22
0

Since you are casting it correctly there's no difference between the two.

But the cast is generally frowned upon in C since a void* can be implicitly converted into any other (data) pointer type.

Also see this C FAQ entry: What's wrong with casting malloc's return value?

A third one is there:

struct node *tempNode = malloc(sizeof *tempNode);

which is better since it makes for less changes in case the type is changed.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Isn't the first sentence contradicting the said in the link? – Eugene Sh. Nov 22 '16 at 19:01
  • @EugeneSh. I included the link *precisely* for all the other relevant points. If you are talking about "forgot to include.." it's not a valid C program (implicit int is no longer allowed). Any decent compiler would issue a warning for that anyway. I don't see the full code from OP. So, I assume OP did include it. For all I know, there might have be `#define malloc NULL` if you are down to that level of nit-picking. – P.P Nov 22 '16 at 19:08
  • 1
    Assuming the first commenter to be a downvoter is a *Post hoc ergo propter hoc*. And it is not a nit-picking. Casting the result of `malloc` is a well known "no-no". – Eugene Sh. Nov 22 '16 at 19:11
  • @EugeneSh. It *is*. (despite what I said in the answer an linking it to C FAQ which explains the same). Should I edit to change "frowned upon" to "no-no"? – P.P Nov 22 '16 at 19:16
  • My first comment was relating to the first sentence of your answer, which can be interpreted as a summary of the rest. You can leave this answer as is, it's up to you, and I promise I won't downvote it. But as you can see there are people who will. – Eugene Sh. Nov 22 '16 at 19:19
  • @EugeneSh. You might as well pick just the "no difference" alone. Poeple are free to downvote as they please with or without a reason, I suppose. And the anonymous downvoter probably didn't bother to read anything else in the answer (or may be just read the "no" in the first sentence). Anyways, I am done here. – P.P Nov 22 '16 at 19:22
-1

With the call to malloc, memory is allocated that has the size of the struct node in bytes. It returns a void* to the allocated memory.

Now the thing is: both of your lines do exactly the same, but the second line will lead to a warning when compiled with a C++ compiler.

C allows implicit conversion of a void* to a different pointer type, whereas the C++ compiler will - at least - warn you, or give you an error.

maxdev
  • 2,491
  • 1
  • 25
  • 50
  • This is not a C++ question, or java, C#, B*f etc. – Weather Vane Nov 22 '16 at 19:03
  • @WeatherVane so what's your point? In C there is obviously no difference, but it is very likely that the questioner want's to know about this, C++ is far more related to C than anything else. – maxdev Nov 22 '16 at 19:07
  • I am sorry but C++ is not on the table, not matter how "related" you think it is. In C if you think there is "obviously no difference" then you have not read the [link already posted](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). I was tempted to DV but gave you the opportunity to retract the answer. – Weather Vane Nov 22 '16 at 19:11
  • Yeah, I read that; it still makes absolutely no difference to functioning of the code if you cast or not. – maxdev Nov 22 '16 at 19:27
  • No, but it makes a difference to the compiler. – Weather Vane Nov 22 '16 at 19:29
  • Thanks for the downvote and your lack of knowledge, and no, it *doesn't* make a difference to the C compiler. – maxdev Nov 22 '16 at 19:38
  • I am saying your answer is off topic - would have been better as a comment, if at all. – Weather Vane Nov 22 '16 at 19:47