0

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...

4 Answers4

5

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);
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
  • @user2408804 I'm not sure what you're trying to build so it's really hard to say, but it looks like you're implementing a linked list. The memory allocation part looks syntactically correct, but you're not actually linking the list, which results in invalid pointers in `first`, `last`, and the fact that `struct ko` gets allocated `SIZE` times instead of the `struct node`. The code I helped you with was a literal solution to changing `struct ko[10]` into `struct ko[SIZE]`, but your initial code was also wrong. – Tom van der Woerdt May 22 '13 at 21:58
  • Yes it's an array of linked lists, with original static allocation of the array the program ran fine, but i can't seem to get it to work with the dynamic allocation.. anyway, thanks for the reply – user2408804 May 23 '13 at 08:24
3

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);
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
0

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);
unwind
  • 391,730
  • 64
  • 469
  • 606
-1

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.

siddstuff
  • 1,215
  • 4
  • 16
  • 37