Passing arrays from function to function

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Passing arrays from function to function

3,940 次查看
NZ_Design
Contributor I
I have a array that I have sussessfully managed to pass to a function. It is a single dimension array.
I know wnont to pass a two dimentional array.
 
my array is decleared as follows
 
  CARDSETTINGS const OC_Layout[28] =
                                    {{  USED,MAX7301_Config_7_4,  MAX7301_Port_Input_PU,1},
                                     {  USED,MAX7301_Config_7_4,  MAX7301_Port_Input_PU,2},
                                     {  USED,MAX7301_Config_7_4,  MAX7301_Port_Output  ,3},
                                     {UNUSED,MAX7301_Config_7_4,  MAX7301_Port_Input_PU,4},
                                     {  USED,MAX7301_Config_11_8, MAX7301_Port_Output  ,1},
                                     .........
                                     {  USED,MAX7301_Config_31_28,MAX7301_Port_Output  ,3},
                                     {  USED,MAX7301_Config_31_28,MAX7301_Port_Output  ,4}};
 
myf function is decleared as
 
extern void  MAX7301_Init     (CARDSETTINGS const* Layout, SPI_PORT Port, volatile uchar* pAddr);
and as called as follows
 
MAX7301_Init(OC_Layout,Sol_Port,Sol_Addr[cCard].pSol);
It appears that it only passes the first array of the array.
 
what have I decleared wrong.
 
 
 
标签 (1)
0 项奖励
回复
7 回复数

2,036 次查看
bigmac
Specialist III
Hello,
 
I assume that the type CARDSETTINGS is actually defined as a structure, so you would actually have an array of structures, rather than a two-dimensional array.
 
You are correct when you observe that passing a pointer to the array corresponds with the address of the first element of the array.
 
To access the n-th element of the array from within the MAX7301_Init function, this should correspond with Layout[n], which is a structure of type CARDSETTINGS.  Is this what you are doing?
 
Regards,
Mac
 
0 项奖励
回复

2,036 次查看
NZ_Design
Contributor I
Yes CARDSETTINGS is a structure.
 
 if (Layout[cIO].bUsed = TRUE) etc for example.
 
This always points to the first item cIO never moves. It is in a for loop.
 
if i write OC_Layout.bUsed it does go through. But I need to be able to pass several different tables of differnt setting.
 
 
 
 
0 项奖励
回复

2,036 次查看
bigmac
Specialist III
Hello,


NZ-Design wrote:
Yes CARDSETTINGS is a structure.
 
 if (Layout[cIO].bUsed = TRUE) etc for example.
 
This always points to the first item cIO never moves. It is in a for loop.
 
if i write OC_Layout.bUsed it does go through. But I need to be able to pass several different tables of differnt setting.
 

Are you saying that the array used for the test is not determined by the value of cIO?  This doesn't seem to make sense.  Perhaps you could try the following code for debug purposes, and see what address the contents of temp contains on each passage of the loop:
 
CARDSETTINGS temp;
 
...
 
for (cIO = 0; cIO < 28; cIO++) {
   temp = Layout[cIO];
   if (temp.bUsed == TRUE) {
 
      ...
 
   }
}
 
Regards,
Mac
 
0 项奖励
回复

2,036 次查看
NZ_Design
Contributor I
Soory Its all working fine.
 
for (cIO = 0; cIO < 5; cIO++)
  if (Layout[cIO].bUsed == USED)
 ...
 
 
works fine. Nothing would work to start with. But it turns out had missed a line on earlier for the turning the device on.
 
This through me as the Hiwave debugger dosent change its values as it should as you step through cIO. But in the execution it works fine. Obviusly a fault with the debugger not being able to interprit what I was doing.
 
Thanks for all your help though.
0 项奖励
回复

2,036 次查看
JimDon
Senior Contributor III
Quite often the compiler will assign local variables as register variable, and the debugger may or may not be able to show the value correctly. Also, when the compiler realizes that the variable is no longer used, the register will get used for something else, and you may see some other value.
This can also apply to function arguments.

While debugging it might be helpful to declare the variable as volatile while debugging, as this seems to stop the compiler for optimizing in this way .

0 项奖励
回复

2,036 次查看
Lundin
Senior Contributor IV
if (Layout[cIO].bUsed = TRUE)

You accidently used = instead of the == operator. If your compiler didn't give you an error for that line, get a new compiler, since you are accidently assigning a value to a const variable.
0 项奖励
回复

2,036 次查看
NZ_Design
Contributor I
Thats a typo in my question. The code definatly has ==.
 
But that dosnt help with my question.
 
0 项奖励
回复