how to do malloc to array from struct

Yael

I have a stract and there should be an array whose size I don't know yet in main I try. Here, for example, I created a define N, but in fact, I accept different data in different ways, including the array W. I have to allocate memory for my array arr of structs.

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

#define N 10

struct vector {
    int size;
    int *arr;
    int length;
};

void main(void) {  
    int w[N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    struct vector *ptr;
    ptr->arr = (int *)malloc(sizeof(int) * N);
    if (ptr->arr == NULL) 
        printf("Unable to allocate memory :(\n");

    for (int i = 0; i < N; i++) {
        ptr->arr[i] = w[i];
        printf("%d ", ptr->arr[i]);
    }
    printf("\n");
}

Tried to do it in different ways but nothing works. gives an error in the terminal: Segmentation fault (core dumped). please help me

Sara Awwad

you need to allocate the ptr first with your structure type. right now ptr=Null, and you need it to point to a memory location with the size of your structure before you can allocate ptr->arr.

try this:

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

#define N 10

typedef struct vector {
   int size;
   int *arr;
   int length;
}vector_t;

void main(void)
{  
    int w[N]={1,2,3,4,5,6,7,8,9};
    vector_t *ptr=(vector_t *)malloc(sizeof(vector_t));

    ptr->arr=(int *) malloc( sizeof(int)*N );
    /* ---- the rest of your code--- */
    free(ptr->arr);
    free(ptr);


}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I use malloc for an array of structs inside another struct that has been created using malloc

From Dev

how to write in a file a struct that has an array with Malloc

From Dev

How do I properly allocate an array within a struct with malloc and realloc in C?

From Dev

Malloc 'ing array of struct in struct

From Dev

Malloc an array inside a struct

From Dev

c malloc array of struct

From Dev

How do I return an array of struct from a function?

From Dev

How do I append an item from an array that was made using a struct?

From Dev

How do i copy a struct from an array of structs?

From Dev

How do you use malloc of struct inside a function?

From Dev

malloc of array in struct passed as argument

From Dev

c++ malloc for array in struct

From Dev

Malloc and memcpy struct plus array

From Dev

Do we have to malloc a struct?

From Dev

How do you allocate memory when reading from a file for a dma struct in a struct array in c

From Dev

How to cast from malloc to array of pointers in C

From Dev

C, How to malloc the correct amount of space for an array of a struct inside another struct?

From Dev

How to correctly malloc a struct in C

From Dev

How malloc char pointer when this one is from struct nested in other struct

From Dev

How to extract an array from a struct array?

From Dev

How Do I Display A List Of Values From An Array Of A Customer Type From Struct [Swift]

From Dev

Passing malloc struct array through a function

From Dev

Dynamic array inside struct and malloc fail

From Dev

Problem with setting the array of pointers in the struct using malloc

From Dev

How do I initialize an array in a struct

From Dev

How do I pass an array of struct to shader

From Dev

How do I allocate memory for a struct with an array?

From

Nested array of struct initialization: how to do if constructors for both struct are available?

From Dev

In Athena how do I query a member of a struct in an array in a struct?

Related Related

  1. 1

    How do I use malloc for an array of structs inside another struct that has been created using malloc

  2. 2

    how to write in a file a struct that has an array with Malloc

  3. 3

    How do I properly allocate an array within a struct with malloc and realloc in C?

  4. 4

    Malloc 'ing array of struct in struct

  5. 5

    Malloc an array inside a struct

  6. 6

    c malloc array of struct

  7. 7

    How do I return an array of struct from a function?

  8. 8

    How do I append an item from an array that was made using a struct?

  9. 9

    How do i copy a struct from an array of structs?

  10. 10

    How do you use malloc of struct inside a function?

  11. 11

    malloc of array in struct passed as argument

  12. 12

    c++ malloc for array in struct

  13. 13

    Malloc and memcpy struct plus array

  14. 14

    Do we have to malloc a struct?

  15. 15

    How do you allocate memory when reading from a file for a dma struct in a struct array in c

  16. 16

    How to cast from malloc to array of pointers in C

  17. 17

    C, How to malloc the correct amount of space for an array of a struct inside another struct?

  18. 18

    How to correctly malloc a struct in C

  19. 19

    How malloc char pointer when this one is from struct nested in other struct

  20. 20

    How to extract an array from a struct array?

  21. 21

    How Do I Display A List Of Values From An Array Of A Customer Type From Struct [Swift]

  22. 22

    Passing malloc struct array through a function

  23. 23

    Dynamic array inside struct and malloc fail

  24. 24

    Problem with setting the array of pointers in the struct using malloc

  25. 25

    How do I initialize an array in a struct

  26. 26

    How do I pass an array of struct to shader

  27. 27

    How do I allocate memory for a struct with an array?

  28. 28

    Nested array of struct initialization: how to do if constructors for both struct are available?

  29. 29

    In Athena how do I query a member of a struct in an array in a struct?

HotTag

Archive