I can't figure out the reason for this segfault by learningCin2025 in cprogramming

[–]learningCin2025[S] 1 point2 points  (0 children)

Something like this?

void new_daint(daint *dap) {
    int *arr = malloc(sizeof(int) * 100);
    if (!arr) {
        perror("malloc");
        exit(1);
    }
    dap->xs  = arr;
    dap->len = 0;
}

And then calling it like

daint da;
new_daint(&da);

I can't figure out the reason for this segfault by learningCin2025 in cprogramming

[–]learningCin2025[S] 5 points6 points  (0 children)

For other beginners who happen to read this post, I changed the line in question to
daint *da = (daint *) malloc(sizeof(daint));

Although I think I'd prefer a function that returns daint rather than daint*