f



pointers, pointers, pointers...

Hi There,

I came up with following sample code to demonstrate my issue:
#include <18F87K22.h>
#device HIGH_INTS=TRUE, adc=16, ICD=TRUE
#fuses NOWDT                      //No Watch Dog Timer
#fuses WDT128                     //Watch Dog Timer uses 1:128
#fuses HSM                        //Hi-Speed crystal oscillator
#fuses NOBROWNOUT                 //No brownout reset
#fuses NOPLLEN                    //No PLL enabled
#fuses BBSIZ1K                    //1K words Boot Block size
#fuses NOXINST                    //Extended set extension and
Indexed

typedef struct{
	int8 foo,
	test;
} TheStruct;

TheStruct mystruct[5];
int16 myarr[2];

void Func(TheStruct *data, int16 *arr);

void main (void)
{
myarr[0]=0xff;
myarr[1]=0xaa;
memset(&mystruct,0,sizeof(mystruct));
Func(myarr,&mystruct);
while(true);

}
//------------------------------------------------------------------------------

void Func(TheStruct *strdat, int16 *arr)
{
	strdat[0]->foo=(*arr[0]);
	strdat[0]->test=(*arr[1]);
}

This compiles just fine (CCS 4.119) but doesn't work, after the
function, foo and test are still 0 cause myarr doesn't seem to be
passed correctly to the function.... what am I doing wrong here? :(

Thanks,
Ron
0
ron.eggler (243)
4/7/2011 7:10:23 PM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

12 Replies
1383 Views

Similar Articles

[PageSpeed] 32

In <6549b0ed-900d-4279-b737-d8fa66831d94@r3g2000yqh.googlegroups.com> cerr <ron.eggler@gmail.com> writes:

> TheStruct mystruct[5];
> int16 myarr[2];

> void Func(TheStruct *data, int16 *arr);

> Func(myarr,&mystruct);

Func is defined to take arguments of pointer-to-TheStruct and
pointer-to-int16.

However calling Func(), you pass the arguments in reverse order:
array-of-int16 and array-of-TheStruct.

Is this intentional?

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

0
gordon16 (684)
4/7/2011 7:24:24 PM
On Apr 7, 12:24=A0pm, John Gordon <gor...@panix.com> wrote:
> In <6549b0ed-900d-4279-b737-d8fa66831...@r3g2000yqh.googlegroups.com> cer=
r <ron.egg...@gmail.com> writes:
>
> > TheStruct mystruct[5];
> > int16 myarr[2];
> > void Func(TheStruct *data, int16 *arr);
> > Func(myarr,&mystruct);
>
> Func is defined to take arguments of pointer-to-TheStruct and
> pointer-to-int16.
>
> However calling Func(), you pass the arguments in reverse order:
> array-of-int16 and array-of-TheStruct.
>
> Is this intentional?
Hoops, no it of course isn't... too much playing around is the cause
of that, consider below version as the one that's causing the issue:
void main (void)
{
TheStruct mystruct[2];
int16 myarr[2];

myarr[0]=3D0xff;
myarr[1]=3D0xaa;
memset(&mystruct,0,sizeof(mystruct));
Func(&mystruct, myarr);
while(true);

}
//-------------------------------------------------------------------------=
-----

void Func(TheStruct *strdat, int16 *arr)
{
	strdat[0]->foo=3D(*arr[0]);
	strdat[0]->test=3D(*arr[1]);
}

Thanks,
Ron
0
ron.eggler (243)
4/7/2011 7:36:51 PM
On 04/ 8/11 07:10 AM, cerr wrote:
> Hi There,
>
> I came up with following sample code to demonstrate my issue:
> #include<18F87K22.h>
> #device HIGH_INTS=TRUE, adc=16, ICD=TRUE
> #fuses NOWDT                      //No Watch Dog Timer
> #fuses WDT128                     //Watch Dog Timer uses 1:128
> #fuses HSM                        //Hi-Speed crystal oscillator
> #fuses NOBROWNOUT                 //No brownout reset
> #fuses NOPLLEN                    //No PLL enabled
> #fuses BBSIZ1K                    //1K words Boot Block size
> #fuses NOXINST                    //Extended set extension and
> Indexed
>
> typedef struct{
> 	int8 foo,
> 	test;
> } TheStruct;
>
> TheStruct mystruct[5];
> int16 myarr[2];
>
> void Func(TheStruct *data, int16 *arr);
>
> void main (void)
> {
> myarr[0]=0xff;
> myarr[1]=0xaa;
> memset(&mystruct,0,sizeof(mystruct));
> Func(myarr,&mystruct);
> while(true);
>
> }
> //------------------------------------------------------------------------------
>
> void Func(TheStruct *strdat, int16 *arr)
> {
> 	strdat[0]->foo=(*arr[0]);
> 	strdat[0]->test=(*arr[1]);
> }
>
> This compiles just fine (CCS 4.119) but doesn't work, after the
> function, foo and test are still 0 cause myarr doesn't seem to be
> passed correctly to the function.... what am I doing wrong here? :(

Using a poor compiler?  With the appropriate headers and typedefs added:

"/tmp/x.c", line 24: warning: argument #1 is incompatible with prototype:
         prototype: pointer to struct  {char foo, char test} : 
"/tmp/x.c", line 17
         argument : pointer to short
"/tmp/x.c", line 24: warning: argument #2 is incompatible with prototype:
         prototype: pointer to short : "/tmp/x.c", line 17
         argument : pointer to array[5] of struct  {char foo, char test}
"/tmp/x.c", line 32: left operand of "->" must be pointer to struct/union
"/tmp/x.c", line 32: cannot dereference non-pointer type
"/tmp/x.c", line 33: left operand of "->" must be pointer to struct/union
"/tmp/x.c", line 33: cannot dereference non-pointer type

-- 
Ian Collins
0
ian-news (10155)
4/7/2011 7:40:24 PM
On 04/ 8/11 07:36 AM, cerr wrote:
> On Apr 7, 12:24 pm, John Gordon<gor...@panix.com>  wrote:
>> In<6549b0ed-900d-4279-b737-d8fa66831...@r3g2000yqh.googlegroups.com>  cerr<ron.egg...@gmail.com>  writes:
>>
>>> TheStruct mystruct[5];
>>> int16 myarr[2];
>>> void Func(TheStruct *data, int16 *arr);
>>> Func(myarr,&mystruct);
>>
>> Func is defined to take arguments of pointer-to-TheStruct and
>> pointer-to-int16.
>>
>> However calling Func(), you pass the arguments in reverse order:
>> array-of-int16 and array-of-TheStruct.
>>
>> Is this intentional?
> Hoops, no it of course isn't... too much playing around is the cause
> of that, consider below version as the one that's causing the issue:
> void main (void)
> {
> TheStruct mystruct[2];
> int16 myarr[2];
>
> myarr[0]=0xff;
> myarr[1]=0xaa;
> memset(&mystruct,0,sizeof(mystruct));
> Func(&mystruct, myarr);
> while(true);
>
> }
> //------------------------------------------------------------------------------
>
> void Func(TheStruct *strdat, int16 *arr)
> {
> 	strdat[0]->foo=(*arr[0]);
> 	strdat[0]->test=(*arr[1]);

These should still fail to compile.  You can't dereference an integer 
(*arr[1]).

-- 
Ian Collins
0
ian-news (10155)
4/7/2011 7:41:38 PM
On Apr 7, 12:40=A0pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 04/ 8/11 07:10 AM, cerr wrote:
>
>
>
>
>
>
>
>
>
> > Hi There,
>
> > I came up with following sample code to demonstrate my issue:
> > #include<18F87K22.h>
> > #device HIGH_INTS=3DTRUE, adc=3D16, ICD=3DTRUE
> > #fuses NOWDT =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0//No Watch Dog =
Timer
> > #fuses WDT128 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 //Watch Dog Timer=
 uses 1:128
> > #fuses HSM =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0//Hi-Speed cr=
ystal oscillator
> > #fuses NOBROWNOUT =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 //No brownout reset
> > #fuses NOPLLEN =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0//No PLL enabled
> > #fuses BBSIZ1K =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0//1K words Boot B=
lock size
> > #fuses NOXINST =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0//Extended set ex=
tension and
> > Indexed
>
> > typedef struct{
> > =A0 =A0int8 foo,
> > =A0 =A0test;
> > } TheStruct;
>
> > TheStruct mystruct[5];
> > int16 myarr[2];
>
> > void Func(TheStruct *data, int16 *arr);
>
> > void main (void)
> > {
> > myarr[0]=3D0xff;
> > myarr[1]=3D0xaa;
> > memset(&mystruct,0,sizeof(mystruct));
> > Func(myarr,&mystruct);
> > while(true);
>
> > }
> > //---------------------------------------------------------------------=
---- -----
>
> > void Func(TheStruct *strdat, int16 *arr)
> > {
> > =A0 =A0strdat[0]->foo=3D(*arr[0]);
> > =A0 =A0strdat[0]->test=3D(*arr[1]);
> > }
>
> > This compiles just fine (CCS 4.119) but doesn't work, after the
> > function, foo and test are still 0 cause myarr doesn't seem to be
> > passed correctly to the function.... what am I doing wrong here? :(
>
> Using a poor compiler? =A0With the appropriate headers and typedefs added=
:
>
> "/tmp/x.c", line 24: warning: argument #1 is incompatible with prototype:
> =A0 =A0 =A0 =A0 =A0prototype: pointer to struct =A0{char foo, char test} =
:
> "/tmp/x.c", line 17
> =A0 =A0 =A0 =A0 =A0argument : pointer to short
> "/tmp/x.c", line 24: warning: argument #2 is incompatible with prototype:
> =A0 =A0 =A0 =A0 =A0prototype: pointer to short : "/tmp/x.c", line 17
> =A0 =A0 =A0 =A0 =A0argument : pointer to array[5] of struct =A0{char foo,=
 char test}
> "/tmp/x.c", line 32: left operand of "->" must be pointer to struct/union
> "/tmp/x.c", line 32: cannot dereference non-pointer type
> "/tmp/x.c", line 33: left operand of "->" must be pointer to struct/union
> "/tmp/x.c", line 33: cannot dereference non-pointer type
>

I got it now,

But why does it work like this:
void Func(TheStruct *strdat, int16 *arr)
{
	strdat[0].foo=3Darr[0];
	strdat[0].test=3Darr[1];
}
strdat is an address passed to the struct & arr is an address to the
array,
why am I not required to dereference these to get the value?

Ron

0
ron.eggler (243)
4/7/2011 7:55:44 PM
On 04/ 8/11 07:55 AM, cerr wrote:
>
> I got it now,
>
> But why does it work like this:
> void Func(TheStruct *strdat, int16 *arr)
> {
> 	strdat[0].foo=arr[0];
> 	strdat[0].test=arr[1];
> }
> strdat is an address passed to the struct&  arr is an address to the
> array,
> why am I not required to dereference these to get the value?

You are dereferencing. arr[0] === *arr and arr[1] === *(arr+1).

-- 
Ian Collins
0
ian-news (10155)
4/7/2011 8:01:08 PM
In <c58cff95-51cd-4c54-bf9a-94e3956b76c3@d2g2000yqn.googlegroups.com> cerr <ron.eggler@gmail.com> writes:

> int16 myarr[2];

> myarr[0]=0xff;
> myarr[1]=0xaa;
> Func(&mystruct, myarr);

> void Func(TheStruct *strdat, int16 *arr)
> {
> 	strdat[0]->foo=(*arr[0]);
> 	strdat[0]->test=(*arr[1]);
> }

In Func(), this line:

  strdat[0]->foo=(*arr[0]);

says "copy the value that is pointed-to by arr[0] into strdat[0]->foo".
But arr[0] isn't a pointer; it's the hexadecimal value FF (int 255).

Did you mean to just copy the value of arr[0] into strdat[0]->foo ?

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

0
gordon16 (684)
4/7/2011 8:05:23 PM
On Apr 7, 1:01=A0pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 04/ 8/11 07:55 AM, cerr wrote:
>
>
>
> > I got it now,
>
> > But why does it work like this:
> > void Func(TheStruct *strdat, int16 *arr)
> > {
> > =A0 =A0strdat[0].foo=3Darr[0];
> > =A0 =A0strdat[0].test=3Darr[1];
> > }
> > strdat is an address passed to the struct& =A0arr is an address to the
> > array,
> > why am I not required to dereference these to get the value?
>
> You are dereferencing. arr[0] =3D=3D=3D *arr and arr[1] =3D=3D=3D *(arr+1=
).
>
> --
> Ian Collins

Hmmm, trying to understand but I still have more problems, I've been
trying and trying, and trying but just can't get it going
correctly... :(
this is from my real app:

typedef struct {
int8 PhaseHi;
int8 PhaseLo;
int8 WidthHi;
int8 WidthLo;
int8 IntensLo;
int8 IntensHi;
int8 BrDaHi;
int8 BrDaLo;
int8 Mode;
int8 HWver;
int8 FWver;
int8 temp;
}MCU1Dat;

MCU1Dat LEDdata[2];


then in main() i call a function:

RecReply (&Status,&MCU1present, LEDdata); //int32 RecReply(CommStruct
*data, int1 *present, MCU1Dat *MCU1LEDdat)

which again calls a function from within:

MCU1Parse (recv_data, MCU1LEDdat);//int32 MCU1Parse(char *str, MCU1Dat
*LEDdata)

and in MCU1LEDdat I change the values like this:

LEDdata[str[3]]->BrDaLo =3D str[6];
LEDdata[str[3]]->BrDaHi =3D str[7]; like this:

For some reason, My vaues aren't present back in main(). Why not,
where am i loosing track?
I put some printf()s in my code trying to get it but hasn't really
helped much further... any help would be greatly appreciated!
Thanks a lot!
0
ron.eggler (243)
4/7/2011 9:48:08 PM
On 04/ 8/11 09:48 AM, cerr wrote:
>
> Hmmm, trying to understand but I still have more problems, I've been
> trying and trying, and trying but just can't get it going
> correctly... :(
> this is from my real app:
>
> typedef struct {
> int8 PhaseHi;
> int8 PhaseLo;
> int8 WidthHi;
> int8 WidthLo;
> int8 IntensLo;
> int8 IntensHi;
> int8 BrDaHi;
> int8 BrDaLo;
> int8 Mode;
> int8 HWver;
> int8 FWver;
> int8 temp;
> }MCU1Dat;
>
> MCU1Dat LEDdata[2];
>
>
> then in main() i call a function:
>
> RecReply (&Status,&MCU1present, LEDdata); //int32 RecReply(CommStruct
> *data, int1 *present, MCU1Dat *MCU1LEDdat)
>
> which again calls a function from within:
>
> MCU1Parse (recv_data, MCU1LEDdat);//int32 MCU1Parse(char *str, MCU1Dat
> *LEDdata)
>
> and in MCU1LEDdat I change the values like this:
>
> LEDdata[str[3]]->BrDaLo = str[6];
> LEDdata[str[3]]->BrDaHi = str[7]; like this:
>
> For some reason, My vaues aren't present back in main(). Why not,
> where am i loosing track?
> I put some printf()s in my code trying to get it but hasn't really
> helped much further... any help would be greatly appreciated!
> Thanks a lot!

You should post a more complete example, all I can guess is something is 
being passed by value where it should be passed by address.  Try 
printing the address of the data structure in each function.

-- 
Ian Collins
0
ian-news (10155)
4/7/2011 9:55:15 PM
On Apr 7, 2:55=A0pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 04/ 8/11 09:48 AM, cerr wrote:
>
>
>
>
>
>
>
>
>
>
>
> > Hmmm, trying to understand but I still have more problems, I've been
> > trying and trying, and trying but just can't get it going
> > correctly... :(
> > this is from my real app:
>
> > typedef struct {
> > int8 PhaseHi;
> > int8 PhaseLo;
> > int8 WidthHi;
> > int8 WidthLo;
> > int8 IntensLo;
> > int8 IntensHi;
> > int8 BrDaHi;
> > int8 BrDaLo;
> > int8 Mode;
> > int8 HWver;
> > int8 FWver;
> > int8 temp;
> > }MCU1Dat;
>
> > MCU1Dat LEDdata[2];
>
> > then in main() i call a function:
>
> > RecReply (&Status,&MCU1present, LEDdata); //int32 RecReply(CommStruct
> > *data, int1 *present, MCU1Dat *MCU1LEDdat)
>
> > which again calls a function from within:
>
> > MCU1Parse (recv_data, MCU1LEDdat);//int32 MCU1Parse(char *str, MCU1Dat
> > *LEDdata)
>
> > and in MCU1LEDdat I change the values like this:
>
> > LEDdata[str[3]]->BrDaLo =3D str[6];
> > LEDdata[str[3]]->BrDaHi =3D str[7]; like this:
>
> > For some reason, My vaues aren't present back in main(). Why not,
> > where am i loosing track?
> > I put some printf()s in my code trying to get it but hasn't really
> > helped much further... any help would be greatly appreciated!
> > Thanks a lot!
>
> You should post a more complete example, all I can guess is something is
> being passed by value where it should be passed by address. =A0Try
> printing the address of the data structure in each function.

Fail! The address of the structure-array is everywhere, in every
function, the same :(


0
ron.eggler (243)
4/7/2011 10:25:51 PM
On Apr 7, 3:25=A0pm, cerr <ron.egg...@gmail.com> wrote:
> On Apr 7, 2:55=A0pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > On 04/ 8/11 09:48 AM, cerr wrote:
>
> > > Hmmm, trying to understand but I still have more problems, I've been
> > > trying and trying, and trying but just can't get it going
> > > correctly... :(
> > > this is from my real app:
>
> > > typedef struct {
> > > int8 PhaseHi;
> > > int8 PhaseLo;
> > > int8 WidthHi;
> > > int8 WidthLo;
> > > int8 IntensLo;
> > > int8 IntensHi;
> > > int8 BrDaHi;
> > > int8 BrDaLo;
> > > int8 Mode;
> > > int8 HWver;
> > > int8 FWver;
> > > int8 temp;
> > > }MCU1Dat;
>
> > > MCU1Dat LEDdata[2];
>
> > > then in main() i call a function:
>
> > > RecReply (&Status,&MCU1present, LEDdata); //int32 RecReply(CommStruct
> > > *data, int1 *present, MCU1Dat *MCU1LEDdat)
>
> > > which again calls a function from within:
>
> > > MCU1Parse (recv_data, MCU1LEDdat);//int32 MCU1Parse(char *str, MCU1Da=
t
> > > *LEDdata)
>
> > > and in MCU1LEDdat I change the values like this:
>
> > > LEDdata[str[3]]->BrDaLo =3D str[6];
> > > LEDdata[str[3]]->BrDaHi =3D str[7]; like this:
>
> > > For some reason, My vaues aren't present back in main(). Why not,
> > > where am i loosing track?
> > > I put some printf()s in my code trying to get it but hasn't really
> > > helped much further... any help would be greatly appreciated!
> > > Thanks a lot!
>
> > You should post a more complete example, all I can guess is something i=
s
> > being passed by value where it should be passed by address. =A0Try
> > printing the address of the data structure in each function.
>
> Fail! The address of the structure-array is everywhere, in every
> function, the same :(

Ok, let me try to come up with a more complete example:

header.h:
//--------------------------------------------------------
typedef struct{
  int8 cmd;
  int16 timeout;
  int16 time_cnt;
  int8 byte_cnt;
} CommStruct;

typedef struct {
int8 PhaseHi;
int8 PhaseLo;
int8 WidthHi;
int8 WidthLo;
int8 IntensLo;
int8 IntensHi;
int8 BrDaHi;
int8 BrDaLo;
int8 Mode;
int8 HWver;
int8 FWver;
int8 temp;
}MCU1Dat;
int32 MCU1Parse(char *str);
int32 RecReply(CommStruct *data, int1 *present, MCU1Dat *CommDat);

code.c
//--------------------------------------------------------
void main()
{
....
MCU1Dat LEDdata[2];
....
  for ( i=3D0;
      ( i < 10 ) &&
	  ( ( LEDdata[0].Mode =3D=3D 0x00 ) ||
	    ( LEDdata[0].BrDaLo=3D=3D 0x00 ) );
	    i++ ){

	// request board status

    setupMCU1 (0x15,0);
    RecReply (&Status,&MCU1present, LEDdata);

    // request Bright/Dark setting
    setupMCU1 (0x17,0);
    RecReply (&Status,&MCU1present, LEDdata);
  }
....
....

int32 MCU1Parse(char *str, MCU1Dat *LEDdata)
{
....
....
      switch (str[2])
      {
....
....
        case 0x17:

        fprintf(PC,"=3D=3D=3D0x%x\r\n", LEDdata);
        fprintf(PC, "str[6] 0x%x\r\n",str[6]);
        fprintf(PC, "LEDdata[%d]->BrDaLo 0x%x\r
\n",str[3],LEDdata[str[3]]->BrDaLo);
        LEDdata[str[3]]->BrDaLo =3D str[6];
        fprintf(PC, "LEDdata[%d]->BrDaLo 0x%x\r
\n",str[3],LEDdata[str[3]]->BrDaLo);
        LEDdata[str[3]]->BrDaHi =3D str[7];

        break;
      }
....
....
}

int32 RecReply(CommStruct *data, int1 *present, MCU1Dat *MCU1LEDdat)
  {
....
....
    if ( data->byte_cnt >=3D 9)
    {
	  fprintf(PC,"=3D=3D=3D0x%x\r\n", MCU1LEDdat);
      //when whole packet received,
      fprintf(PC,"MCU1LEDdat[0]->BrDaLo 0x%x\r\n",MCU1LEDdat[0]-
>BrDaLo);
      value =3D MCU1Parse (recv_data, MCU1LEDdat); //parse !
      fprintf(PC,"MCU1LEDdat[0]->BrDaLo 0x%x\r\n",MCU1LEDdat[0]-
>BrDaLo);
....
....
    }
....
....
  }
I hope this displays my problem better.
Thank you very much for your efforts! Assistance is greatly
appreciated!
Thanks!
0
ron.eggler (243)
4/7/2011 10:56:08 PM
On Apr 7, 3:56=A0pm, cerr <ron.egg...@gmail.com> wrote:
> On Apr 7, 3:25=A0pm, cerr <ron.egg...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > On Apr 7, 2:55=A0pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
> > > On 04/ 8/11 09:48 AM, cerr wrote:
>
> > > > Hmmm, trying to understand but I still have more problems, I've bee=
n
> > > > trying and trying, and trying but just can't get it going
> > > > correctly... :(
> > > > this is from my real app:
>
> > > > typedef struct {
> > > > int8 PhaseHi;
> > > > int8 PhaseLo;
> > > > int8 WidthHi;
> > > > int8 WidthLo;
> > > > int8 IntensLo;
> > > > int8 IntensHi;
> > > > int8 BrDaHi;
> > > > int8 BrDaLo;
> > > > int8 Mode;
> > > > int8 HWver;
> > > > int8 FWver;
> > > > int8 temp;
> > > > }MCU1Dat;
>
> > > > MCU1Dat LEDdata[2];
>
> > > > then in main() i call a function:
>
> > > > RecReply (&Status,&MCU1present, LEDdata); //int32 RecReply(CommStru=
ct
> > > > *data, int1 *present, MCU1Dat *MCU1LEDdat)
>
> > > > which again calls a function from within:
>
> > > > MCU1Parse (recv_data, MCU1LEDdat);//int32 MCU1Parse(char *str, MCU1=
Dat
> > > > *LEDdata)
>
> > > > and in MCU1LEDdat I change the values like this:
>
> > > > LEDdata[str[3]]->BrDaLo =3D str[6];
> > > > LEDdata[str[3]]->BrDaHi =3D str[7]; like this:
>
> > > > For some reason, My vaues aren't present back in main(). Why not,
> > > > where am i loosing track?
> > > > I put some printf()s in my code trying to get it but hasn't really
> > > > helped much further... any help would be greatly appreciated!
> > > > Thanks a lot!
>
> > > You should post a more complete example, all I can guess is something=
 is
> > > being passed by value where it should be passed by address. =A0Try
> > > printing the address of the data structure in each function.
>
> > Fail! The address of the structure-array is everywhere, in every
> > function, the same :(
>
> Ok, let me try to come up with a more complete example:
>
> header.h:
> //--------------------------------------------------------
> typedef struct{
> =A0 int8 cmd;
> =A0 int16 timeout;
> =A0 int16 time_cnt;
> =A0 int8 byte_cnt;
>
> } CommStruct;
>
> typedef struct {
> int8 PhaseHi;
> int8 PhaseLo;
> int8 WidthHi;
> int8 WidthLo;
> int8 IntensLo;
> int8 IntensHi;
> int8 BrDaHi;
> int8 BrDaLo;
> int8 Mode;
> int8 HWver;
> int8 FWver;
> int8 temp;}MCU1Dat;
>
> int32 MCU1Parse(char *str);
> int32 RecReply(CommStruct *data, int1 *present, MCU1Dat *CommDat);
>
> code.c
> //--------------------------------------------------------
> void main()
> {
> ...
> MCU1Dat LEDdata[2];
> ...
> =A0 for ( i=3D0;
> =A0 =A0 =A0 ( i < 10 ) &&
> =A0 =A0 =A0 =A0 =A0 ( ( LEDdata[0].Mode =3D=3D 0x00 ) ||
> =A0 =A0 =A0 =A0 =A0 =A0 ( LEDdata[0].BrDaLo=3D=3D 0x00 ) );
> =A0 =A0 =A0 =A0 =A0 =A0 i++ ){
>
> =A0 =A0 =A0 =A0 // request board status
>
> =A0 =A0 setupMCU1 (0x15,0);
> =A0 =A0 RecReply (&Status,&MCU1present, LEDdata);
>
> =A0 =A0 // request Bright/Dark setting
> =A0 =A0 setupMCU1 (0x17,0);
> =A0 =A0 RecReply (&Status,&MCU1present, LEDdata);
> =A0 }
> ...
> ...
>
> int32 MCU1Parse(char *str, MCU1Dat *LEDdata)
> {
> ...
> ...
> =A0 =A0 =A0 switch (str[2])
> =A0 =A0 =A0 {
> ...
> ...
> =A0 =A0 =A0 =A0 case 0x17:
>
> =A0 =A0 =A0 =A0 fprintf(PC,"=3D=3D=3D0x%x\r\n", LEDdata);
> =A0 =A0 =A0 =A0 fprintf(PC, "str[6] 0x%x\r\n",str[6]);
> =A0 =A0 =A0 =A0 fprintf(PC, "LEDdata[%d]->BrDaLo 0x%x\r
> \n",str[3],LEDdata[str[3]]->BrDaLo);
> =A0 =A0 =A0 =A0 LEDdata[str[3]]->BrDaLo =3D str[6];
> =A0 =A0 =A0 =A0 fprintf(PC, "LEDdata[%d]->BrDaLo 0x%x\r
> \n",str[3],LEDdata[str[3]]->BrDaLo);
> =A0 =A0 =A0 =A0 LEDdata[str[3]]->BrDaHi =3D str[7];
>
> =A0 =A0 =A0 =A0 break;
> =A0 =A0 =A0 }
> ...
> ...
>
> }
>
> int32 RecReply(CommStruct *data, int1 *present, MCU1Dat *MCU1LEDdat)
> =A0 {
> ...
> ...
> =A0 =A0 if ( data->byte_cnt >=3D 9)
> =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 fprintf(PC,"=3D=3D=3D0x%x\r\n", MCU1LEDdat);
> =A0 =A0 =A0 //when whole packet received,
> =A0 =A0 =A0 fprintf(PC,"MCU1LEDdat[0]->BrDaLo 0x%x\r\n",MCU1LEDdat[0]->Br=
DaLo);
>
> =A0 =A0 =A0 value =3D MCU1Parse (recv_data, MCU1LEDdat); //parse !
> =A0 =A0 =A0 fprintf(PC,"MCU1LEDdat[0]->BrDaLo 0x%x\r\n",MCU1LEDdat[0]->Br=
DaLo);
>
> ...
> ...
> =A0 =A0 }
> ...
> ...
> =A0 }
> I hope this displays my problem better.
> Thank you very much for your efforts! Assistance is greatly
> appreciated!
> Thanks!

Uh, I got it!
Got all confused with my struct being an array of a struct and thus []
is already dereferencing my addresses...cleared-up my mind and got it
going... thx for sticking thru with me...! Still appreciated! Thanks
guys!

roN
0
ron.eggler (243)
4/7/2011 11:17:18 PM
Reply: