malloc and how properly to use-it
hi friends,
I am reading this book: Amazon.com: C Programming for the Absolute Beginner (For the Absolute Beginner (Series).) (9781931841528): Michael Vine: Books
And to be on right track I test things, think a bit... and when I have some questions I try to post here sine we have many advance users...
The author claims this things about malloc, which I would like to hear your opinion.
1. Basic malloc() use is demonstrated in the below.
Code:
char *name;
name = malloc(80);
2. The preceding program’s use of malloc() is not quite complete because some C compilers may require that you perform type casting when assigning dynamic memory to a variable.
Code:
char *name;
name = (char *) malloc(80);
3. Better yet, we should be more specific when creating dynamic memory by explicitly telling the system the size of the data type for which we are requesting memory.
Code:
char *name;
name = (char *) malloc(80 * sizeof(char));
Is the third way the right way to use malloc always? Do you agree... Are you personally using it like that? Please let me know further, I really would like to hear your valuable opinion!