How to make a user defined array of struct in C

不羁岁月 提交于 2019-12-13 04:23:58

问题


I would like the user to define the size of the array when the program starts, I currently have:

#define SIZE 10
typedef struct node{
    int data;
    struct node *next;
} node;

    struct ko {
    struct node *first;
    struct node *last;
} ;

struct ko array[SIZE];

This works, however, I would like to remove the #define SIZE, and let SIZE be a value that the user defines, so in the main function i have:

int SIZE;
printf("enter array size");
scanf("%d", &SIZE);

how can I get that value to the array?

EDIT: now i have the following in the .h file:

    typedef struct node{
    int data;
    struct node *next;
    } node;

    struct ko {
    struct node *first;
    struct node *last;
    } ;

struct ko *array;
int size;

and this in the main.c file:

printf("size of array: ");
scanf("%d", &size);
array = malloc(sizeof(struct ko) * size);

Should this work? It doesn't the program crashes but I don't know if the problem is here or, elsewhere in the program...


回答1:


Instead of struct ko array[SIZE];, dynamically allocate it :

struct ko *array;
array = malloc(sizeof(struct ko) * SIZE);

Make sure to free it once you're done with it :

free(array);



回答2:


Declare array as a pointer and dynamically allocate the needed memory using malloc:

struct ko* array;

int SIZE;
printf("enter array size");
scanf("%d", &SIZE);

array = malloc(sizeof(struct ko) * SIZE);

// don't forget to free memory at the end
free(array);



回答3:


You can use dynamic memory allocation, using the malloc() library function:

struct ko *array = malloc(SIZE * sizeof *array);

Note that using ALL CAPS for a variable is very rare in C, style-wise it's quite confusing.

When you are done with memory allocated this way, pass the pointer to the free() function to de-allocate the memory:

free(array);



回答4:


The size of array is defined at compile time , C doesn't allow us to specify the size of array at run time . This is called static memory allocation. This can be useful when the data we are dealing with is static in nature. But can not always deal with static data. When we have to store a data which is dynamic in nature means size of data changes at run time , static memory allocation can be a problem.

To resolve this issue we can use dynamic memory allocation. It allow us to define size at run time. It allocate us a memory block at anonymous location of requested size and type. The only way to use this memory block is by pointer. malloc() function is used for dynamic memory allocation and it return us a pointer which can be used to access the allocated location.

Example-

Suppose we are dealing with integer type values, the numbers of integers is not fixed , is dynamic.

Using int type array to store these values will not be efficient.

  int A[SIZE];

Dynamic memory allocation.

  int *A;
  A = (int*) malloc(SIZE * sizeof(int));

NOTE: Similar concept is applied for struct. Becomes allocated dynamic memory can be of any type.



来源:https://stackoverflow.com/questions/16688372/how-to-make-a-user-defined-array-of-struct-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!