Pointers...

Hi all,

I’m not really sure what to make of this. I just hope it has a simple explanation haha.

I have the following function:

int splitRecDataByComma(int *data_deposit_array)
{
  int i = 0;
  char buff[6];
  int * ptr_data_deposit[4];

  //Assign pointers to the deposit
  ptr_data_deposit[0] = data_deposit_array;
  ptr_data_deposit[1] = (data_deposit_array + 1);
  ptr_data_deposit[2] = (data_deposit_array + 2);
  ptr_data_deposit[3] = (data_deposit_array + 3);

  //Do something with the pointers e.g.
  *ptr_data_deposit[1] = atoi(buff);
}

Ignoring what the function actually does, this works just fine. The purpose of the pointers is that I will be changing the number of variables passed over in the array ‘data_deposit_array’.

However, I now change the pointer declarations to the following and it falls over:

int splitRecDataByComma(int *data_deposit_array)
{
  int i = 0;
  char buff[6];
  int * ptr_data_deposit[4];

  //Assign pointers to the deposit
  for(i=0;i<4;i++){
    ptr_data_deposit[i] = data_deposit_array + i;
  }

  //Do something with the pointers e.g.
  *ptr_data_deposit[1] = atoi(buff);
}

I don’t get why it doesn’t work… Does anyone know what am I missing??

Much appreciated

should work as I can’t see a bug in the part of the code you publish -

  • you forgot the () to make it 100% identical but that should not make a difference
for(i=0;i<4;i++){
    ptr_data_deposit[i] = (data_deposit_array + i);
  }

Thanks for the reply; it’s nice to know I’m not going mad!

I’ve played around with it some more and am just as confused. I tried what you suggested again and that doesn’t work either.

Curiously the following does:

int splitRecDataByComma(int *data_deposit_array)
{
  int i = 0;
  char buff[6];  

  //Do something with the pointers e.g.
  for(i=0;i<4;i++){
    //Lots of other code...
    *(data_deposit_array + i) = atoi(buff);
    //... lots of other code
  }
}

This is exactly the same thing again. I’m wondering if it’s a timing issue or something really weird.

That aside, the above snippet of code achieves what I wanted originally because I can change how many loops.