i have fullnames
, 2d array has sorted full names in , want copy content sortednames
, 2d array exists out side of function. (i ***sortednames
parameter).
i dynamically allocated array, copying not succeed. program crashes after 4th attempt copy name fullnames
sortednames
. why?
stringcpy
, stringlen
functions created. same thing strcpy
, strlen
does.
/*allocating memory sortednames*/ *sortednames = (char**) malloc(n);/*n number of names*/ /*allocating memory each sortednames array*/ (i = 0; < n; i++) { (*sortednames)[i] = (char*) malloc(stringlen(fullnames[i])+1); } /*copying fullnames sortednames*/ (i = 0; < n; i++) { stringcpy((*sortednames)[i],fullnames[i]); }
you not allocate enough memory array of pointers, should allocate way:
*sortednames = (char**)malloc(n * sizeof(char *));
furthermore, why not use strlen
, strcpy
in place of stringlen
, stringcpy
? typo or these function perform function?
regarding cast on malloc
return value, remove if not intend compile code c++ , write this:
*sortednames = malloc(n * sizeof(**sortednames));
regarding parentheses around **sortednames
, aware not necessary can remove them or not depending on local style conventions.
Comments
Post a Comment