Go Down

Topic: Pointers... (Read 892 times) previous topic - next topic

websitedragon

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:
Code: [Select]
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:
Code: [Select]
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

robtillaart

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

Code: [Select]

for(i=0;i<4;i++){
    ptr_data_deposit[i] = (data_deposit_array + i);
  }
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

websitedragon

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:
Code: [Select]
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.



Go Up