|
|
View previous topic :: View next topic |
Author |
Message |
rougie
Joined: 14 Jun 2006 Posts: 31
|
Storing an array subscript to a member of a structure |
Posted: Mon Jul 27, 2009 11:18 am |
|
|
Hello,
I have tried the following code in VC++ compiler as a .c module and it compiles without error or warnings and works the way it supposed to. Now, I know that C for PICs does not have the same conformity as C for desktops and might not follow the same compliance rules. So for this reason I am asking this forum if the following code should be coded with slightly different syntax in order to be accomodated to PIC C.
Here is the code:
Code: |
#include <Dv4685.h> // DEVICE INNITIALIZATIONS
#include <stdlib.h> // CCS LIBRARY STANDARD LIBRARY
#include <stdlibm.h> // CCS LIBRARY (HEAP) MALLOC MODULE
typedef struct tag_lb
{
long (*pmr)[5]; <<< Error points here!
}lb;
lb* create_obj(long (*pmr)[5])
{
lb *ptr_lb = NULL;
ptr_lb = malloc (sizeof *ptr_lb);
ptr_lb->pmr = pmr;
return ptr_lb;
}
void f1(lb *pObj)
{
long t,x=0,y=0;
t = pObj->pmr[x][y];
t = pObj->pmr[0][1];
t = pObj->pmr[0][2];
}
int main()
{
lb *pObj = NULL;
long pmr[][5] = { {195, 194, 193, 111, 111 },
{194, 195, 112, 222, 111 } };
pObj = create_obj(&pmr[0]);
f1(pObj);
free(pObj);
return 0;
} |
Any input as to why I am getting the following error in PCWH CCS compiler version 4.093:
Quote: | Error 28 "Kernel.c" Line 8 (6,7): Expecting an identifier |
All feedback is greatly appreciated!
Best regards
Robert |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 27, 2009 12:48 pm |
|
|
It may need typedef'ing of the function pointer before you use it.
See the example at the end of this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=37478
I was able to make it compile by using that method:
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
// Typedef a function pointer.
typedef long (*_fptr)(int8);
typedef struct tag_lb
{
_fptr pmr[5];
}lb;
//======================================
void main(void)
{
while(1);
} |
I don't guarantee that any of this works. I don't want to spend a lot
of time working on this. I've done it in the past and there are posts
in the archives on this (function pointers). I just wanted to help you
get past this initial sticking point. A lot of experimentation may be
required to get it to work. |
|
|
rougie
Joined: 14 Jun 2006 Posts: 31
|
|
Posted: Mon Jul 27, 2009 2:06 pm |
|
|
PCM Programmer,
My question is not related to function pointers. What I am trying to do is pass in the two dimensional array's base address (or subscript) into a function via a pointer of type
long (*pmr)[5]
and then storing this pointer value to the "long (*pmr)[5]; " member of the tag_lb structure. Then, once in the f1() function, I want to retrieve the pointer so I can use it to access the two dimensional array's informations.
Regards
Roberto |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|