View previous topic :: View next topic |
Author |
Message |
monsun
Joined: 17 Jan 2012 Posts: 17
|
Problem with pointers |
Posted: Thu Mar 21, 2013 2:11 am |
|
|
Sometimes i have problems with big arrays and pointers:
for example when i use it this way everything is allright:
float example(float *something_external)
{
int8 i;
float something;
for(i=0;i<10;i++)
{
something+=*something_external;
something_external++;
}
return something;
}
BUT when i use it this way sometimes it makes some problems
float example(float *something_external)
{
int8 i;
float something;
for(i=0;i<10;i++)
{
something+=*(something_external+i);
}
return something;
}
Any ideas? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Thu Mar 21, 2013 2:20 am |
|
|
Incorrect syntax.....
You want.
Code: |
something+=(*something_external)+i;
|
You are saying to add 'i' to the pointer, _then_ take the contents of this. Effectively treating it as an array, so
Code: |
something+=something_external[i];
|
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
monsun
Joined: 17 Jan 2012 Posts: 17
|
|
Posted: Thu Mar 21, 2013 2:44 am |
|
|
Ive thought that something_external[i] is equal to *(something_external+i) using pointers?? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Thu Mar 21, 2013 3:15 am |
|
|
OK. So you do want to use it as an array?.
Question then is compiler version and chip???.
If you want to use it as an array, then 'yes' this is right, _but only if you are on a modern compiler_. Historically the older compilers did not increment pointers correctly, treating their size as '1', whatever they pointed to.
There were also problems on particular chips when you crossed page boundaries.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|