1

I need to increase length of 2 arrays according to user input. I'm using the code below. But output is not matching with user input.

    #include<stdio.h>        
    int main()
    {
        int i=0, key=0, size[key], time[key];

        while (key!=-1)
        {
             printf("Insert value for size : ");
             scanf("%d",&size[i]);
             printf("Insert value for time : ");
             scanf("%d",&time[i]);
             i++;

             printf("Run again Press-1 or Exit  : ");
             scanf("%d",&key);
        }

        int x=0;
        for (x ; x<i ; x++)
        {
             printf("%d %d\n",size[x],time[x]);
        }
        return 0;
    }

When user inputs the values:

    35 4
    20 3
    40 1
    60 7
    20 8

Then this is the output:

    8  4
    20 3
    40 1
    60 7
    20 8

If length of array exceeded 4, the last element of the time array will be printed as the first element of the size array.

Why does my program give this output?

Alex Johnson
  • 958
  • 8
  • 23
Lakmal Geekiyanage
  • 25
  • 1
  • 1
  • 10
  • 2
    That's not how any of this works. Look up `realloc()` if you want to allocate a resizeable array. – EOF Sep 16 '15 at 21:58
  • What part of your code do you think is defining the initial size of these arrays, and what is that size? What part do you think is changing that size? – Scott Hunter Sep 16 '15 at 21:59
  • Also, size[key] and time[key] are declared when key is zero, meaning there are no items available in the array, it's zero size. I'd expect your program to crash – JVene Sep 16 '15 at 22:00
  • How do I use realloc() function? – Lakmal Geekiyanage Sep 16 '15 at 22:19
  • You use `realloc` to change the size of memory allocated with `malloc` or `calloc`. – Weather Vane Sep 16 '15 at 22:46
  • @pistachiobk array dimensions do not need to be constants. However using `0` causes undefined behaviour. – M.M Sep 16 '15 at 23:28
  • @M.M As I read C11 spec `malloc(0)` is Unspecified behavior, not undefined behavior. "Whether the `calloc`, `malloc`, and `realloc` functions return a null pointer or a pointer to an allocated object when the size requested is zero" §J.3.12 – chux - Reinstate Monica Sep 17 '15 at 01:50
  • @chuk I refer to `int key = 0, size[key];` as in the question – M.M Sep 17 '15 at 01:51

1 Answers1

2

Something to get you started.

Use realloc() to re-size the memory pointer to by a pointer.

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

int main(void) {
  // Start with NULL and a size of 0
  int *my_time = NULL;
  size_t sz = 0;

  while (!some_exit_condition) {
    long long ll; 
    printf("Insert value for size : ");
    if (scanf("%lld", &ll) != 1) {
      puts("Numeric input missing");
      break;
    }
    if (ll < 0 || ll > SIZE_MAX) {
      puts("Size out of range");
      break;
    }

    sz = (size_t) ll;
    void *new_ptr = realloc(my_time, sz * sizeof *my_time);
    if (new_ptr == NULL && sz != 0) {
      puts("Out of memory");
      break;
    }
    my_time = new_ptr;

    // do something with `my_time` and `sz`

  }
  // Clean-up
  free(my_time);
  my_time = NULL;
  sz = 0;

  return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256