0
typedef struct node {
    int data;
    struct node *next;
} node;

node *new;
new = malloc(sizeof node);  // syntax error
new = malloc(sizeof(node)); // working fine

I am trying to assign memory to a new node dynamically using malloc function, my DOUBT is, why the first statement is giving syntax error? The only difference between the two statement is that in the second one node is inside brackets.

Sam Protsenko
  • 14,045
  • 4
  • 59
  • 75
Sumit Sagar
  • 127
  • 9
  • 3
    The type name is required brackets. – BLUEPIXY May 16 '15 at 10:43
  • 1
    see [n1256.pdf](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) **A.2.1 Expressions (6.5.3)** – BLUEPIXY May 16 '15 at 10:50
  • [duplicate](http://stackoverflow.com/questions/5894892/why-and-when-do-i-need-to-use-parentheses-after-sizeof) – Sam Protsenko May 16 '15 at 10:53
  • please don't write `sizeof(type)`. Use `sizeof(*variable)` instead, for safety reasons. and most definitely do **NOT** call your variable `new`, it's a C++ keyword, and there are people who are trying to compile C code with a C++ compiler. – The Paramagnetic Croissant May 16 '15 at 12:25

0 Answers0