pointers, pointers, pointers...

C

cerr

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
 
J

John Gordon

In said:
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?
 
C

cerr

In said:
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
 
I

Ian Collins

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

Ian Collins

In said:
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]).
 
C

cerr

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 Timeruses 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
 
I

Ian Collins

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).
 
J

John Gordon

In said:
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 ?
 
C

cerr

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).

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!
 
I

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!

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.
 
C

cerr

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 :(
 
C

cerr

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 :(

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=0;
( i < 10 ) &&
( ( LEDdata[0].Mode == 0x00 ) ||
( LEDdata[0].BrDaLo== 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,"===0x%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 = str[6];
fprintf(PC, "LEDdata[%d]->BrDaLo 0x%x\r
\n",str[3],LEDdata[str[3]]->BrDaLo);
LEDdata[str[3]]->BrDaHi = str[7];

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

int32 RecReply(CommStruct *data, int1 *present, MCU1Dat *MCU1LEDdat)
{
....
....
if ( data->byte_cnt >= 9)
{
fprintf(PC,"===0x%x\r\n", MCU1LEDdat);
//when whole packet received,
fprintf(PC,"MCU1LEDdat[0]->BrDaLo 0x%x\r\n",MCU1LEDdat[0]-
value = MCU1Parse (recv_data, MCU1LEDdat); //parse !
fprintf(PC,"MCU1LEDdat[0]->BrDaLo 0x%x\r\n",MCU1LEDdat[0]-
....
....
}
....
....
}
I hope this displays my problem better.
Thank you very much for your efforts! Assistance is greatly
appreciated!
Thanks!
 
C

cerr

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 somethingis
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 :(

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=0;
      ( i < 10 ) &&
          ( ( LEDdata[0].Mode == 0x00 ) ||
            ( LEDdata[0].BrDaLo== 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,"===0x%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 = str[6];
        fprintf(PC, "LEDdata[%d]->BrDaLo 0x%x\r
\n",str[3],LEDdata[str[3]]->BrDaLo);
        LEDdata[str[3]]->BrDaHi = str[7];

        break;
      }
...
...

}

int32 RecReply(CommStruct *data, int1 *present, MCU1Dat *MCU1LEDdat)
  {
...
...
    if ( data->byte_cnt >= 9)
    {
          fprintf(PC,"===0x%x\r\n", MCU1LEDdat);
      //when whole packet received,
      fprintf(PC,"MCU1LEDdat[0]->BrDaLo 0x%x\r\n",MCU1LEDdat[0]->BrDaLo);

      value = 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!

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top