Alex wrote:
Herbert Rosenau wrote:
><ly*******@hotmail.tomwrote:
>>Simply strange, I found "define" can not work with "malloc".
Together my complier will say "parse error".
Could anyone tell me why?
-------------------------
#define MAX 10000
......
int main(void){
....
char * temp=(char *)malloc(MAX*sizeof(char));
I think the error is you define the point "temp" after some
code you omitted. Maybe it's the reason of "parse error"
If MAX is a magnitude that will not overstrain the automatic
storage (i.e. overload the stack in most implementations) temp
should be simply defined as:
char temp[MAX];
since it is in the main function, and exists for the life of the
program (assuming no recursive calls to main). Otherwise the
proper way to malloc it is:
char *temp; /* in the declarations */
...
temp = malloc(MAX * sizeof *temp);
which is proof against alteration of the type of temp, or against
insertion of code in the ... segment. It also gives the compiler
the maximum chance of complaining about failure to #include
<stdlib.h>, which is fatal, yet hidden by an unnecessary cast.
--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.netUSE maineline address!