help me !~~~
how to use malloc!~~
thank you
help me !~~~
how to use malloc!~~
thank you
Alright, I'll post some code even though there are a million sites that could help you with this one.
it is better practice to do this though:Code:int x = malloc(5*sizeof(int)); //will work on many new compilers...
Code:int x = (int)malloc(5*sizeof(int));
Last edited by master5001; 09-24-2001 at 01:15 AM.
Interesting....
I would use:
int *x;
x=(int *)malloc(5*sizeof(int));
alex
Casting the return result of malloc masks your failure to include stdlib.h. In some systems (where pointers are larger than ints, or pointers are returned in different registers?), this WILL cause your code to break.
Since malloc returns void* in ANSI-C (and void* is the universal pointer type which can be silently cast into any other pointer type), this is (imo) correct
Code:#include <stdlib.h> ... int *arr = malloc( 5 * sizeof(int) );
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
what about
int *arr;
arr=malloc(5*sizeof(*arr));
so that if type of arr changes you dont have to worry .