View previous topic :: View next topic |
Author |
Message |
d00dajo
Joined: 20 Jul 2004 Posts: 34
|
Pointers |
Posted: Tue Mar 22, 2005 4:37 am |
|
|
Hi again,
Many Q's when using a new compiler ;-)
I have gotten myself into trouble when trying to use pointers to arrays in the CCS compiler. Surely I am doing someting I am not supposed to.
Example:
MyMain
{
int8 myVar[8];
myVar[0] = 0x99;
MyFunction(myVar);
}
MyFunction(*int8 my_array)
{
int8 localVar;
localVar = my_array[0];
}
Now, localVar will NOT get the value 0x99.
Obviously, the argument I am passing is not the adress as I intended it to be. Probably a very simple programmers error, would someone care to bail me out?
//Daniel |
|
|
Ttelmah Guest
|
|
Posted: Tue Mar 22, 2005 5:30 am |
|
|
Actually the argument you are passing is the address you are expecting. It is the declaration in the function, that is wrong. The syntax should be:
MyFunction(int8 *my_array)
Which says that 'my_array' is a pointer to an int8.
Best Wishes |
|
|
d00dajo
Joined: 20 Jul 2004 Posts: 34
|
Correction |
Posted: Tue Mar 22, 2005 6:12 am |
|
|
Ttelmah wrote: | Actually the argument you are passing is the address you are expecting. It is the declaration in the function, that is wrong. The syntax should be:
MyFunction(int8 *my_array)
Which says that 'my_array' is a pointer to an int8.
Best Wishes |
Hi Ttelmah, thx for your response,
Sorry the syntax in the example was a typo.
Actually it was supposed to be:
MyFunction(int8* my_array)
The thing is, I have found that the function actually DOES work. But only when I am in one of the lower function calls.
More exactly:
Global: int8 array[8];
void main(void)
{
array[0]=0x99;
myFunction(array);
}
This will work fine. However, the following code WILL NOT WORK:
void main(void)
{
...
Function1();
}
void Function1()
{
...
Function2();
}
void Function2()
{
...
array[0]=0x99;
myFunction(array);
}
This seems very strange, since the dataarraystructure is global so it should not matter where in the code I make the call (since it cannot be stack-related when I set the value just prevoius to the call)
I realise this isnt really a simple question to ask, when all of the code is not known. But ideas are greatly appreciated. |
|
|
|