[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

pointers, pointers, pointers...

cerr

4/7/2011 7:10:00 PM

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
12 Answers

John Gordon

4/7/2011 7:24:00 PM

0

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"

cerr

4/7/2011 7:37:00 PM

0

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]);
}

Thanks,
Ron

Ian Collins

4/7/2011 7:40:00 PM

0

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

Ian Collins

4/7/2011 7:42:00 PM

0

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

cerr

4/7/2011 7:56:00 PM

0

On Apr 7, 12:40 pm, 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=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
>

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?

Ron

Ian Collins

4/7/2011 8:01:00 PM

0

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

John Gordon

4/7/2011 8:05:00 PM

0

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"

cerr

4/7/2011 9:48:00 PM

0

On Apr 7, 1:01 pm, 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)
> > {
> >    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

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!

Ian Collins

4/7/2011 9:55:00 PM

0

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

cerr

4/7/2011 10:26:00 PM

0

On Apr 7, 2:55 pm, 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 = 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.

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