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 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

12 Replies
1236 Views

Similar Articles

[PageSpeed] 42

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:

Similar Artilces:

Pointer-to-pointer-to-pointer question
The code example below shows the dynamic allocation of a 2D array. I must admit that it took quite a while for me to get there (I already have another posting to that effect), but I am glad that I finally got it working. Now here's the problem: I am able to get the 2D array dynamically allocated correctly as long as I am doing it "in-line" (i.e. without invoking any function). The moment I try to do it in another function, I get a core dump. Any help will be appreciated. Since this function is expected to update a pointer-to-pointer type, I am actually passing the pointer-to-pointer-to-pointer type to the function. What am I missing here? You can see that the source code works correctly when I am perform 2D array initialization "in-line" (i.e. by not invoking a function call) simply by un-commenting the line /* #define INLINE_INIT */ Masood /******************************************************/ #include <stdio.h> #include <stdlib.h> /*#define INLINE_INIT*/ #define MAXROWS 3 #define MAXCOLS 5 void buildTbl(int ***tblPtr, size_t numRows, size_t numCols) { *tblPtr = (int **)malloc(numRows*sizeof(int*)); /* C++ : *tblPtr = new (int*)[numRows]; */ for(size_t i = 0; i < numCols; i++) *tblPtr[i] = (int *)malloc(numCols*sizeof(int)); /* C++: *tblPtr[i] = new (int)[numCols]; */ } main() { int startVal = 5; int **tbl; #ifdef INLINE_INIT tbl = (int **)malloc(MAXROWS*sizeof(int*)); /* C++ : tbl = new (int*)[MAXROWS]; */ ...

pointer vs pointer to pointer
AIM: To write a queue with 3 operations. WHAT I GOT: It works PROBLEM: Have a question: Why do "pointer to pointer" in enqueue() and "a pointer" to deleteElement() both work fine ? Will enqueue() work fine if I pass just "a pointer" to it ? Will deleteElement() work fine if I pass "pointer to pointer" ? /* A queue implementataion with the operations that I need: * (1) Add element to the front of the queue * (2) remove an element with a unique ID (string constant) * (3) compare 2 elements (string comparison) * (4) print queue * ...

pointer to a pointer
My situation: I have a function that takes a pointer to an FSref as an argument. In the body of the function is a function call that also takes a pointer to an FSRef. I need to pass my original FSRef, but since I have only a pointer to it, I am assuming I need to dereference it twice when I call the second function. However, using something like &&myFSRef in the second function doesn't work. Can anyone tell me the proper way to do this? Thanks. In article <1105802448.cd52b3255e6c95476ce23f7be215a6a3@teranews>, Steven Daedelus <sdaedelus@ira.gov> w...

pointer to pointer
I have only used pointer to a pointer while manipulating arrays of arrays. I have not seen any C++ libraries that use pointer to a pointer as a function parameter. But I have seen it used in COM. If we want the function to create a new object and allow the passed in pointer to point to the object created we can pass a pointer to the reference. Is there anything special about COM that it needs pointer to a pointer. Is this becuase that COM can be used from C as well and we don't have references in C. In what other circumstances pointer to pointer can be used. Why don't we never see poi...

Pointers for these pointers...?
Hi Everyone: I'm trying to develop a property list to include as metadata about my object classes. i.e. I want each class I'm developing to include a PropertyList which will contain ObjectProperty pointers. ObjectProperty is a class containing the typeid.name() of a type as it's ObjectType as well as an ObjectName and ObjectDescription. Basically 3 strings of metadata describing each of a class' member variables (and hopefully functions) so that the class can report to a query from another class what properties it has available for access at run time. Here is what I have and t...

pointer of pointer
Hi I have a structure, B_t is another structure typedef struct{ int len; unsigned char code[12]; B_t *b; }A_t Now I need to pass A_t into a function to evaluate I use pointer of pointer void evaluate(A_t **a) My question is how to evaluate the component of this structure (*a)->b??? seems not right Thanks a lot! <qianz99@gmail.com> wrote in message news:1178915812.306611.242380@y80g2000hsf.googlegroups.com... > Hi > > I have a structure, B_t is another structure > > typedef struct{ > int len; > unsigned char code[12]; > B_t *b; > }A_t > > Now I need to pass A_t into a function to evaluate > I use pointer of pointer > > void evaluate(A_t **a) > > My question is how to evaluate the component of this structure > > (*a)->b??? > > seems not right > It is right. It is ugly and so you might be better off with A_t *aptr; aptr = *a; aptr->b; Code that a machine can read correctly isn't necessarily easy for humans to read. The extra variable will almost certainly be optimised away by the compiler, so there is unlikely to be any efficiency penalty. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm On May 11, 1:36 pm, qian...@gmail.com wrote: > My question is how to evaluate the component of this structure > > (*a)->b??? > It accesses member b of A_t, which a pointer to B_t. Whether it is right or not depends upon what you mean by 'eva...

pointers to pointers
Hi, i understood what pointers are and all stuff, and now i'm at the part with pointers to pointers and my book doesn't really help on this issue. let be: int a,*p,**q; how to make use of **q,*q and q ? Which who's address and/or who's value contains. I would like some examples like p=&a;// p contains the address of a and *p contains it's value but regarding **q,*q respectively q. Thanks. apropo wrote: > Hi, > i understood what pointers are and all stuff, and now i'm at the part > with pointers to pointers and my book doesn't really help on...

Pointer to Pointer or not?
Hi I am creating my own Queue class to learn about Queues and pointers. I have come across a question of two styles and I don't know if there are any dangers associated with them. I coded my remove function as follows template <class T> bool adsQ<T>::Remove(T* theData) { Qnode<T>* removeNode = 0; if (true == isEmpty()) { //cout << "False" << endl; return false; } else { /* What I am trying to achive here is to change the data pointed to by theData to the data pointed to by pData.*/ *theData =*(pHead->pData); //Data exchan...

pointer to pointer
Its just a very simple and basic program to learn exactly what is described in comments here. Can you provide a better (and short) exercise ? /* A program to learn how to use a pointer to a pointer. Here we take an array of char pointers. A char* in the array, of course, points * to some char. Array has only one element. We change the value of char pointed by array's element using a function. * * VERSION 0.0 */ #include <stdio.h> void replace_char_value(char* [], char); int main(void) { char* arrc[2] = {0}; char c = 'c'; arrc[0] = &c;...

Pointers to pointers
How can you use a "pointer to a pointer" as a two dimensional array using 'new' and 'delete' rather than 'malloc' and 'free'? Thanks, Jonas Jonas Cord wrote: > How can you use a "pointer to a pointer" as a two dimensional array > using 'new' and 'delete' rather than 'malloc' and 'free'? Pretty much the same. int main() { int** data; data = new int*[100]; for (int i = 0; i < 100; ++i) data[i] = new int[100]; //use it for (int i = 0; i < 100; ++i) delete []...

c++: pointers to pointers
Hi, I have an array of pointers defined as such: myArray** ptr = new myArray[10]; for some reason i get a compile time error stating there is no default constructor for myArray. The problem is that i should not need to define one since myArray is a pointer to a pointer and so should not have a default constructor - its a primitive data type and not an object. Any help appreciated. Regards, A A wrote in news:3f9e5e27_1@news.iprimus.com.au: > Hi, > > I have an array of pointers defined as such: > > myArray** ptr = new myArray[10]; myArray** ptr = new myArray *[10]; &g...

Pointer to pointer Vs References to Pointer
Q . When to use use pointer-to-pointer and reference-to-pointer to modify a pointer passed to a function ? According to me Which is better : double pointers OR Reference to Pointer ? There is no clear advantage and depends on programmer =91s choice or habit . As Nilesh mentioned =94 Reference to pointer=94 makes code easy But Pointer to pointer makes code easy to understand. So one can use any one . Why Pointer to Pointer is generally used ? 1. Pointer to pointer can be used in C and C++ but reference to pointer is limited to C++. a. Generally people start learning programming from C . So they get used to pointer to pointer. b.Pointer to Pointer usage makes project porting in C or C++ easy. 2. Generally libraries uses pointer to pointer approach like COM uses pointer to pointer for CoCreateInstance or QueryInterface. Fourm : Could you please let me know if these are the only reasons or i am missing something? Thanks Vikrant Example #include "stdafx.h" void Ref2Poi(int * &p) { *p =3D 10; } void Poi2Poi( int ** p) { **p =3D 10; } int _tmain(int argc, _TCHAR* argv[]) { int i =3D 13; int j =3D 14; int *p =3D &i; int *q =3D &j; Ref2Poi(p); Poi2Poi(&q); return 0; } "bansalvikrant@gmail.com" <bansalvikrant@gmail.com> writes: > Q . When to use use pointer-to-pointer and reference-to-pointer to > modify a pointer passed to a function ? &g...

pointers and array of pointers
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas myswap2 is doing exactly what I want it to do - swaping pointers. Where I made a mistake? Thanks void myswap(char *pa, char *pb){ char *tmp; tmp=pa; pa=pb; pb=tmp; } void myswap2(char *sw[]){ char *tmp; tmp=sw[0]; sw[0]=sw[1]; sw[1]=tmp; } int main(void){ char *a="1111", *b="2222", *A[]={"1111","2222"}; printf("1: %s, 2: %s\n", a,...

pointer to pointer question
struct pair_node { int i; double d; }; struct pair_node **x; The following is what I want to create. x -> [ ] -> (2,0.1) (3,0.2) (-1,?) [ ] -> (2,0.1) (3,0.3) (4,-1.2) (-1,?) [ ] -> (1,0.4) (-1,?) [ ] -> (2,0.1) (4,1.4) (5,0.5) (-1,?) [ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (-1,?) // Do I need an allocation before this loop for x? for(i = 0; i < N; ++i){ //dumps core on the following line, anyone can help me why? // am i missing something? nprob.x[i] = (struct pair_node *)malloc(sizeof(struct pair_node)*D); for(j = 0; j ...

pointer-to-pointer to char
The following code can be compiled. But When I run it, it causes "Segmentation fault". int main(){ char **c1; *c1 = "HOW"; // LINE1 ++(*c1); *c1 = "ARE"; ++(*c1); *c1 = "YOU"; } c1 is a pointer-to-pointer to char. How to initialize c1 to three strings: "HOW", "ARE", "YOU"? Thanks a lot. Jack wrote: > The following code can be compiled. But When I run it, it causes > "Segmentation fault". > > int main(){ > char **c1; > > *c1 = "HOW"; // LINE1 > c1 doesn...

Pointer to Pointer to character
a simple program... #include <iostream> using namespace std; int main(int argc, char** argv) { cout<<argv<<endl; cout<<**argv<<endl; cout<<argv[0][0]<<endl; cout<<&argv[0]<<endl; cout<<&(argv[0][0])<<endl; // why not the same as &argv[0]? } why &argv[0] & &(argv[0][0]) difference? Howachen posted: > #include <iostream> You need to include "ostream" if you want to use "endl". > using namespace std; > > int main(int argc, char** argv) { > > ...

pointer to an array of pointers
Hi, Could anyone please tell me how to dereference a pointer to an array of pointers? Regards On Dec 3, 7:13 pm, ramu <ramu....@gmail.com> wrote: > Hi, > Could anyone please tell me how to dereference a pointer to an > array of pointers? > > Regards *p or p[0]. A pointer to an array of N pointers is char *(*)[N]. Example: char *(*foo)[N] = NULL; "ramu" <ramu.ask@gmail.com> wrote in message news:f0977e82-d97d-4124-8f94-f3addd5b6c8a@s8g2000prg.googlegroups.com... > Hi, > Could anyone please tell me how to dereference a pointer to ...

Confused about pointer on pointer
Hi all, i've a problem with a simple example program: i've a variable: float *vertices; and want a function: initVertices(float **v, int l) which should init the vertices array. It should be filled with values from 0 to l (l should also have the size l) initVertices(float **v, int l){ // ? // ? } int main(int argc, char **argcv){ int l = 10; float *vertices=NULL; initVertices(&vertices, l); //should output 0.0, 1.0, 2.0, ... for(int i=0; i<l; i++) printf("%i, &quo...

pointers to pointers question
How come something like: #include <stdio.h> int main(int argc, char **argv) { char **arg; int i; for ( i = 0; argv[i] != NULL; ++i ) { puts(argv[i]); } for ( arg = argv; *arg != NULL; ++arg ) { puts(*arg); } return 0; } doesn't require a explicit & before argv in the for loop, but something like #include <stdio.h> #include <stdlib.h> int main(void) { char *ptr = "Garbage"; char **pptr; pptr = &ptr; printf("The value is: % s\n", *pptr); } does require an explict & Chad Chad w...

Why pointers to pointers used
I have used many OSS libraries which have a common structure like the following: They have init function which takes a pointer to pointer to the module data structure like this: module_init(struct module_ds ** p,.....); And usage example is like following: struct module_ds * p; p = NULL; module_init(&p,....); etc. Now i'm wondering why they do it this way? if i pass a plain pointer then it should be ok to declare a local pointer and allocate and initialize it. after initialization it can be returned by the fuction. but they always have a void type. Even if they remain "void&...

Pointer to a Pointer to a struct
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { typedef struct { char **test; }Testing; Testing **p; p = malloc(sizeof(Testing)); p[0]->test[0] = malloc(sizeof(Testing)); strncpy(p[0]->test[0],"Hello World",20); printf("%s\n",p[0]->test[0]); return EXIT_SUCCESS; } Why does the following crash ? Not sure what i'm missing please advise thanks. -- Unix Systems Engineer The City of New York Dept. of Information Technology http://www.nyc.gov/doitt rbrown[(@)]doitt.nyc.gov http://www.rodrickb...

pointer to a pointer problems
An open book test I am taking, tasked me to write a function with the following prototype. Yea, its a test, so don't give me the implementation. But it is open book, so maybe I can at least get a few questions answered. // returns the next token in a string found seperated by the delimeter. upon return updates str to point to the next token in // the string after the one returned const char * nextToken(const char ** str, const char delimeter); When I look at this, it looks very c-style to me. I think it is unsafe. 1) I don't see a way to return a different string without allocating ...

Pointers to pointers #3
What do pointers to pointers accomplish? How does having a pointer to a pointer help a design or aglorithm? Where are they normally used? cman In article <1171819744.426037.176610@s48g2000cws.googlegroups.com>, cman <tilakb@gmail.com> wrote: >What do pointers to pointers accomplish? How does having a pointer to >a pointer help a design or aglorithm? Where are they normally used? > >cman > If you want to modify an object passed in the argument list of a function from within the function, you must pass a pointer to the object as the argument. So, if you want to mo...

using pointer to pointer(**)
Hello! I have noticed that if you want to return an object from a method the example I have seen always use **. Can somebody explain why? //Tony "Tony Johansson" <johansson.andersson@telia.com> kirjutas: > Hello! > > I have noticed that if you want to return an object from a method the > example > I have seen always use **. > > Can somebody explain why? > Seems like C code or unjustified premature optimization attempts. In C++ the simplest way to return an object from a function would be: class A {/*...*/}; A f() { return A(); } in...

Web resources about - pointers, pointers, pointers... - comp.lang.c

The Pointer Sisters - Wikipedia, the free encyclopedia
The Pointer Sisters are an American R&B recording girl group from Oakland , California that achieved mainstream success during the 1970s and ...

Michael Pointer (@michaelpointer) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Thread: I'm looking for pointers to a Google Maps API overview.
... outline for a map, making it super-easy to edit, and also to have it in the most convenient form for someone browsing over the data. Any pointers ...

Alpha Range ~ LaserVRâ„¢ laser pointer target shooting game! for iPhone 3GS, iPhone 4, iPhone 4S, iPod ...
Get Alpha Range ~ LaserVRâ„¢ laser pointer target shooting game! on the App Store. See screenshots and ratings, and read customer reviews.

German Shorthaired Pointer Dog Hates Alarm Clock - YouTube
This is my dog Adenine, a German Shorthaired Pointer, waking up to my alarm in the morning. He hates the alarm and howls at it until it turns ...

Shark patrol chopper back in the air - as white pointer closes beaches
Perth's shark patrol helicopter took off for its first flight of the season on Saturday morning - as a great white menaced City Beach and Floreat ...

Paul Gallen's pro boxing debut may be a pointer to the future
Cronulla captain Paul Gallen will confirm at a press conference on Friday that his first professional boxing bout will be on the Geale-Wood undercard ...

Inside mail: All the big bets, market movers, work whispers, trial pointers and quaddies for Rosehill ...
ALL the big bets, market movers, work whispers, trial pointers and quaddie tips for Rosehill on Saturday.

Esperance shark attack survivor Sean Pollard recalls how he fought off white pointer - PerthNow Search ...
BUNBURY shark attack survivor Sean Pollard says he feels lucky to be alive after his miracle escape from a white pointer near Esperance.

Ray Allen Three Pointer - Heat V Spurs - Video
It appeared smooth and effortless, an example of pure "in the moment" sporting perfection.

Resources last updated: 3/15/2016 12:56:37 PM