Pages: [1]   Go Down
Author Topic: Pointers...  (Read 873 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
for(i=0;i<4;i++){
    ptr_data_deposit[i] = (data_deposit_array + i);
  }
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 1
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.


 
Logged

Pages: [1]   Go Up
Jump to: