Question

What have I done wrong here? It falls after I input count and then my array It must be something with memory allocation, but i tried many things and none of them helped. thanks for responding

struct _arr {
   size_t count;
   int* arr;
};

typedef struct _arr array_t;

array_t array_create(int* arr, size_t count){
    array_t* newArr;

    newArr = (array_t*)malloc(sizeof(count)+sizeof(int)*count);
    newArr->count = count;
    newArr->arr = arr;
    return *newArr;
}


array_t array_get(FILE* file){
    int* arr = NULL;
        size_t count;
    array_t arr_t;
    int i = 0;

    if (!file) return;
    if (!fscanf(file, "%u", &count)) return;
    arr_t = array_create(arr, count);

    for (i = 0; i < arr_t.count; i++){
        if (!fscanf(file, "%d", &arr_t.arr[i])) return;
    }

    for (i = 0; i<arr_t.count; i++)
    printf("%d ", arr_t.arr[i]);
    printf("\n");

    return arr_t;
}


int main(){
    array_t arr;
    int i = 0;

    arr = array_get(stdin);

    for (i = 0; i<arr.count; i++)
        printf("%d ", arr.arr[i]);

    getch();
    return 0;
}
Was it helpful?

Solution

WhozCraig's crystal ball says your problem is in array_create(). Specifically, the field arr is not allocated properly. Your array_create() should look something like this:

array_t array_create(int length)
{
    array_t res = {length, NULL};
    if (length > 0) {
        res.arr = malloc(length * sizeof(*res.arr));
        if(res.arr == NULL) {
            printf("malloc(%d) failed\n", length * sizeof(*res.arr));
            exit(1);
        }
    }
    return res;
}

You also need to fix those empty returns. Maybe you just want to print a message and exit.


Update Mon Nov 18 16:13:48 CST 2013

This is a whole different version that allocates an array_t from the heap. The error handling is kind of ugly ( all those places that return 0; ). This should work, the other worked too, but maybe an array_t on the heap is what you are looking for.

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

typedef struct {
   size_t count;
   int *arr;
} array_t;

array_t *array_create(size_t count){
    array_t *newArr = malloc(sizeof(array_t));
    if(newArr == 0) return 0;
    newArr->count = count;
    newArr->arr = calloc(sizeof(int),count);
    if(newArr->arr == 0) return 0;
    return newArr;
}

array_t *array_get(FILE* file){
    size_t count;
    array_t *arr;
    int i = 0;

    if (!file) return 0;
    if (!fscanf(file, "%zu", &count)) return 0;
    arr = array_create(count);
    if(arr == 0) return 0;

    for (i = 0; i < arr->count; i++){
        if (!fscanf(file, "%d", &arr->arr[i])) return 0;
    }

    for (i = 0; i<arr->count; i++)
        printf("%d ", arr->arr[i]);
    printf("\n");

    return arr;
}


int main(void) {
    array_t *arr;
    int i = 0;

    arr = array_get(stdin);
    if(arr == 0) return -1;

    for (i = 0; i<arr->count; i++)
        printf("%d ", arr->arr[i]);

    getch(); // getch is windows
    return 0;
}

OTHER TIPS

according to me you are trying to returning the value that present on that address having pointer. so just simply return newArr

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top