I have the following linked list data structure:
struct _node {
char *text;
stuct _node *next;
}
I want to write a function that converts this linked list into an array of strings with each string being terminated by \n and the whole array terminated by \0.
For example, if the linked list was:
[first]->[second]->NULL
then the array should look like this:
[f][i][r][s][t][\n][s][e][c][o][n][d][\n][\0]
Here is my attempt:
char *convertToArray(struct _node *head){
assert(head != NULL);
int lines = findLines(head);
int i = 0;
struct _node *curr = head;
char *textBufferArray = NULL; // return NULL if lines == 0
textBufferArray = malloc(charCount(head) + lines + 1);
// malloc enough memory for all characters and \n and \0 characters
if (lines > 0){
while (curr->next != NULL){
strlcpy(textBufferArray[i], curr->text, strlen(curr->text)+1);
// I need to add a new line here
curr = curr->next;
i++;
}
}
I need to add \0 before returning
textBufferArray[charCount(head) + lines] = '\0';
return textBufferArray;
}
sizeof(char) *which is just1 *?