1

So I'm trying to do some practice in C by trying to create a dynamic array of structs, but I'm running into some difficulty when trying to pass the struct into different functions for different operations.

My code so far:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct node {
char *str;
int len;
};
//& gives address of value, * gives value at address
int main(void) {
    struct node **strarray = NULL; 
    int count = 0, i = 0;

    printf("hello\n");
    strarray = (struct node **)realloc(strarray, (count + 1) * sizeof(struct node *));

    /* allocate memory for one `struct node` */
    strarray[count] = (struct node *)malloc(sizeof(struct node));

    strarray = init(strarray);  
    return 0;
}

struct node ** init(struct node ** strarray){ //this is the line that's causing problems

    int i = 0, count = 0;
    char line[1024];

    if(fgets(line, 1024, stdin) != NULL) {
        /* add ONE element to the array */
        strarray = (struct node **)realloc(strarray, (count + 1) * sizeof(struct node *));

        /* allocate memory for one `struct node` */
        strarray[count] = (struct node *)malloc(sizeof(struct node));

        /* copy the data into the new element (structure) */
        strarray[count]->str = strdup(line);
        strarray[count]->len = strlen(line);
        count++;
        return **strarray;
    }

}
void printarray(){
    for(i = 0; i < count; i++) {
        printf("--\n");
        printf("[%d]->str: %s", i, strarray[i]->str);
        printf("[%d]->len: %d\n", i, strarray[i]->len);
    }
}

I haven't worked on the printarray method yet, I'm trying to get the function declaration and the pass to work. Currently, I'm getting a conflicting types for ‘init’ struct node** init(struct node** strarray) error which I have tried many fixes to, but no avail.

  • 1
    You should declare your functions before you try to call them. You should not cast the result of `malloc` or `realloc`. – aschepler Mar 29 '17 at 00:12
  • 1
    You should also compile with "gcc -Wall" because you have a lot of warnings – Archmede Mar 29 '17 at 01:03
  • Casting the result of `realloc` is not a good practice. For the reason, [this answer](http://stackoverflow.com/a/605858/5843393) explains it very clear. – Zhigang An Mar 29 '17 at 04:15
  • That's pretty interesting, never knew void already covered it. Thanks! – Isaac Chan Mar 30 '17 at 16:12

1 Answers1

0

You're problem is that you're derefenrecing the variable you're returning. Do

return strarray

Instead of

return **strarray

Here is the whole function:

struct node ** init(struct node ** strarray){

int i = 0, count = 0;
char line[1024];

if(fgets(line, 1024, stdin) != NULL) {
    /* add ONE element to the array */
    strarray = (struct node **)realloc(strarray, (count + 1) * sizeof(struct node *));

    /* allocate memory for one `struct node` */
    strarray[count] = (struct node *)malloc(sizeof(struct node));

    /* copy the data into the new element (structure) */
    strarray[count]->str = strdup(line);
    strarray[count]->len = strlen(line);
    count++;
    return strarray;
}
}
Archmede
  • 1,592
  • 2
  • 20
  • 37
  • Hi, thanks for your response, I've actually tried that fix before, but then it just gets replaced by the error: conflicting types for init in the: struct node** init(struct node** strarray) line – Isaac Chan Mar 29 '17 at 01:12
  • change the return statement itself and not the function definition as stated in my answer @IsaacChan Because I compiled it and I didn't get the error – Archmede Mar 29 '17 at 01:36
  • And your function declaration was the same line? i.e. struct node ** init(struct node ** strarray) Cause I changed the return statement to return strarray; and now I get the conflicting types error. Would you mind posting the code that you got to compile? I'll try to see if there's any differences that I might not have picked up on – Isaac Chan Mar 29 '17 at 01:45
  • Nevermind, 7 hours I spent on this problem and I just realized that the entire time, I never initailized the function...... – Isaac Chan Mar 29 '17 at 02:11
  • Sorry about that, I actually did have the function header declared too @IsaacChan – Archmede Mar 29 '17 at 02:46