3

I have this function where list is a pointer to a linked list. This is the list struct which contains nodes with data.

typedef struct node{
    char *data;
    struct node *next;
}NODE;
typedef struct list{
    NODE* head;
    char *id;
    struct list *next;
    int size;
}LIST;

My goal is to take every data from the list and return it as a **char. It looks like it works but the line where i do words = (char**) calloc(numOfWords*wordLength,sizeof(char)); I'm not sure if it's correct and how it works exactly. Can you see anything that is going wrong in my code and help me? Thanks.

char **reset_words(LIST *list){
    char ** words;
    int i = 0,j = 0,wordLength,numOfWords = list->size;
    NODE *p;
    for (p = list->head; p != NULL; p = p->next) {
        wordLength = my_strlen(p->data);
        words = (char**) calloc(numOfWords*wordLength,sizeof(char));
        for(int k=0;k < wordLength;k++){
            words[i][j] = p->data[k];
            j++;
        }
        i++;
    }
    return words;
}
1
  • In C, there is no need to cast the return of malloc (or calloc, or realloc), it is unnecessary. See: Do I cast the result of malloc? Commented Oct 30, 2020 at 5:09

2 Answers 2

3

After i looked at your code here are some changes:

  1. If your list is a correctly done linked-list and all nodes have the same size of data then you could simply do wordLength = strlen(list->head->data).
  2. Initialize words outside the loops. The way i did it should work.
  3. Since you have string you can simply do strcpy(words[i],p->data) to add the string to your words array.

Try this out let me know if it works.

char **reset_words(LIST *list){
    char ** words;
    int i = 0,j = 0,wordLength = strlen(list->head->data),numOfWords = list->size;
    NODE *p;
    words = (char **)malloc(numOfWords * sizeof(char *));
    for (int h=0; h<numOfWords; h++)
        words[h] = (char *)calloc(wordLength,sizeof(char));
    for (p = list->head; p != NULL; p = p->next) {
        strcpy(words[i],p->data);
        i++;
    }
    return words;
}
Sign up to request clarification or add additional context in comments.

Comments

0
        wordLength = my_strlen(p->data);

I do not know, what my_strlen() does. But you have to keep in mind to reserve additional space for the trailing \0.

        words = (char**) calloc(numOfWords*wordLength,sizeof(char));

This does not make much sense... numOfWords seem to be the number of nodes while wordLength is the length of the actual data. So numOfWords * wordLength is something which might be used to calculate to total amount of space for all characters (but only, when wordLength is the length of the largest string).

Depending on you requirements (do you really need to copy the strings or does a reference suffice?), you can express this as:

char **words = calloc(list->size, sizeof words[0]);
size_t i = 0;
for (struct node *p = list->head; p; p = p->next) {
    if (1)
         words[i++] = p->data;
    else
         words[i++] = strdup(p->data);
}

1 Comment

My strlen() is correct, it does exactly the same thing as the normal strlen(). I assume that all words have the same length. So length of the first is the length for all words. The method should return char[][] or *char[] or **char which is basically trhe same thing. A would like to copy the words, a reference would not help me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.