-2

I don't know how should I type the new resized array. I create this in C...

void main()
 {
  int=1;
  int h[a];
  a++;
  h[a];
  h[1]=2;
  printf("%d",h[1]);
}

1 Answers1

0

You can't do it like that. If you want to dynamically change the size of the array, you would have to use new operator. You have allocate a new array with higher size and then copy all the old elements. Something like below.

int sz = 1;
int[] h = new int[sz];
sz++;
int h1 = new int[sz];
for (int i = 0; i < h.length;++i) {
  h1[i] = h[i];
}
h = h1;
h[1]=2;
printf("%d",h[1]);
vavasthi
  • 922
  • 5
  • 14