Reputation: 285
Can someone explain these portion of code to me please? Correct me if I am wrong.
int *longest = malloc(sizeof(int));
*longest =0;
Does this mean:
malloc(4)
?Upvotes: 2
Views: 168
Reputation: 88711
Yes, it gets exactly enough memory for one int
. You should avoid explicitly assuming a given size - it makes it a nightmare to port to other platforms in the future. You spotted it yourself - sizeof(int)
won't always be 4 everywhere.
No, this assigns the value 0 to the newly allocated memory, which was pointed to by longest
. The *
here is the dereference operator, it informally says "I want to work with the thing this pointer points to".
Upvotes: 6