Dynamic array issues

So I thought I had this down but I guess not.

In order to create a dynamic integer array with a given size SIZE, you do the following:

1
2
3
4
5
6
7
8
  #define SIZE 100
  ...
  int *numbers;
  numbers = new int[SIZE](); // initialize with values zero
  for (unsigned int i = 0; i < SIZE; ++i)
  {
     numbers[i] = i;
  }


This results in a segmentation fault.

However...

1
2
3
4
5
6
7
8
  #define SIZE 100
  ...
  int *numbers;
  numbers = new int[SIZE](); // initialize with values zero
  for (unsigned int i = 0; i < SIZE; ++i)
  {
     *(&numbers[i]) = i;
  }


results in what I want.

But in all the tutorials I've seen around the Internet, you can simply write values by using the [] operator!

Can somebody explain to me what I'm doing wrong?
Thanks
I tried to use both method, and it works fine I can either use [] or *(&var). Maybe somethings wrong with your compiler ?
remove () .

Note also that define is lil evil here , if I was a compiler I would change your code by a
int number[100];

note also you that numbers[ i ] = i ; is the same as *(numbers + i ) = i;
Topic archived. No new replies allowed.