What is the equivalent of new in C?. I apologize for these questions. I am not that familiar with linked lists.
What is the equivalent of new in C?. I apologize for these questions. I am not that familiar with linked lists.
malloc() is the equivalent of new , and free() is the equivalent of delete . For example in C:
And there's also calloc().Code:head = malloc( sizeof(*head) );
calloc() is meant for allocating arrays. It also sets the allocated memory to zero, while malloc() doesn't.
Code:#include <stdio.h> #include <stdlib.h> char *str = malloc(sizeof(char) * 80); strncpy(str, argv[0], 80);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Be careful with that code. If strlen(argv[0]) is greater than 80 then str will end up without a terminating '\0'.
If you understand what you're doing, you're not learning anything.