hello,
i was looking at the code online for strdup and wanted to ask a question regarding its implementation w.r.t malloc()
I wanted to know why is there a need to call malloc on the variable p? wont this function still work without the malloc?Code:strdupmerely copies the string given by its argument into a safe place, obtained by a call onmalloc:char *strdup(char *s) /* make a duplicate of s */ { char *p; p = (char *) malloc(strlen(s)+1); /* +1 for '\0' */ if (p != NULL) strcpy(p, s); return p; }
hope to hear from you soon.