| | |
Malloc a struct
![]() |
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Solved Threads: 0
Hello guys..
I´m trying to do something like the code below ..
in other words I want to alloc dynamically the number of nodes and also the length of s in node struct
I tried a lot of things like:
and my code do not run
Is it possible to do what I want?
thanks.
I´m trying to do something like the code below ..
c Syntax (Toggle Plain Text)
struct node{ int a; char *s; }; void main() { node *x; int sMAX = 3; int nodeMAX = 10; x = (node*)malloc((sizeof(int)+sizeof(char)*sMAX)*nodeMAX); ... }
I tried a lot of things like:
c Syntax (Toggle Plain Text)
x = (node*)malloc((sizeof(node)*nodeMAX); x = (node*)malloc((sizeof(struct node)*nodeMAX); x = (node*)malloc((sizeof(node)+sizeof(char)*sMAX)*nodeMAX);
Is it possible to do what I want?
thanks.
Last edited by Narue; Nov 21st, 2008 at 11:20 am. Reason: added code tags
>I want to alloc dynamically the number of nodes and also the length of s in node struct
Do it separately:
>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:
Do it separately:
c Syntax (Toggle Plain Text)
struct node *x = malloc ( nodeMAX * sizeof *x ); int i; for ( i = 0; i < nodeMAX; i++ ) x->s = malloc ( sMAX );
In C89, not without invoking undefined behavior. In C99 the "struct hack" has been blessed and is directly supported as a flexible array member:
c Syntax (Toggle Plain Text)
struct node{ int a; char s[]; }; struct node *x = malloc ( nodeMAX * ( sizeof *x + sMAX ) );
Last edited by Narue; Nov 21st, 2008 at 1:32 pm.
I'm here to prove you wrong.
![]() |
Similar Threads
- about struct in C (C)
- struct linked list problem (C)
- struct and member function pointer (C)
- Pointers to struct (C++)
- Memory allocation for typedef struct (C++)
Other Threads in the C Forum
- Previous Thread: Sending "char" to "int" variable
- Next Thread: Learning C Language
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() iso keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate lowest match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition reversing scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h






