![]() |
| ||
| Malloc a struct Hello guys.. I´m trying to do something like the code below .. struct node{in other words I want to alloc dynamically the number of nodes and also the length of s in node structI tried a lot of things like: x = (node*)malloc((sizeof(node)*nodeMAX);and my code do not run Is it possible to do what I want? thanks. |
| ||
| Re: Malloc a struct >I want to alloc dynamically the number of nodes and also the length of s in node struct Do it separately: struct node *x = malloc ( nodeMAX * sizeof *x );>Is it possible to do what I want? In C89, not without invoking undefined behavior. In C99 the "struct hack" has been blessed and is directly supported as a flexible array member: struct node{ |
| ||
| Re: Malloc a struct Also, in C, shouldn't node *x;be struct node *x;? Another note - main returns int. |
| ||
| Re: Malloc a struct >Also, in C, shouldn't node *x; be struct node *x; ? Indeed. I've been working in mixed C/C++ too much these days. :icon_rolleyes: |
| All times are GMT -4. The time now is 5:36 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC