pointers, pointers, pointers...

Discussion in 'C Programming' started by cerr, Apr 7, 2011.

  1. cerr

    cerr Guest

    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
    cerr, Apr 7, 2011
    #1
    1. Advertising

  2. cerr

    John Gordon Guest

    In <> cerr <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Apr 7, 2011
    #2
    1. Advertising

  3. cerr

    cerr Guest

    On Apr 7, 12:24 pm, John Gordon <> wrote:
    > In <> cerr <> 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
    cerr, Apr 7, 2011
    #3
  4. cerr

    Ian Collins Guest

    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, Apr 7, 2011
    #4
  5. cerr

    Ian Collins Guest

    On 04/ 8/11 07:36 AM, cerr wrote:
    > On Apr 7, 12:24 pm, John Gordon<> wrote:
    >> In<> cerr<> 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
    Ian Collins, Apr 7, 2011
    #5
  6. cerr

    cerr Guest

    On Apr 7, 12:40 pm, Ian Collins <> 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 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
    cerr, Apr 7, 2011
    #6
  7. cerr

    Ian Collins Guest

    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
    Ian Collins, Apr 7, 2011
    #7
  8. cerr

    John Gordon Guest

    In <> cerr <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Apr 7, 2011
    #8
  9. cerr

    cerr Guest

    On Apr 7, 1:01 pm, Ian Collins <> 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!
    cerr, Apr 7, 2011
    #9
  10. cerr

    Ian Collins Guest

    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
    Ian Collins, Apr 7, 2011
    #10
  11. cerr

    cerr Guest

    On Apr 7, 2:55 pm, Ian Collins <> 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 :(
    cerr, Apr 7, 2011
    #11
  12. cerr

    cerr Guest

    On Apr 7, 3:25 pm, cerr <> wrote:
    > On Apr 7, 2:55 pm, Ian Collins <> 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 :(


    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!
    cerr, Apr 7, 2011
    #12
  13. cerr

    cerr Guest

    On Apr 7, 3:56 pm, cerr <> wrote:
    > On Apr 7, 3:25 pm, cerr <> wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On Apr 7, 2:55 pm, Ian Collins <> 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 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
    cerr, Apr 8, 2011
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    639
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    751
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    438
    Alan Kelon
    Oct 29, 2003
  4. Xamalek
    Replies:
    7
    Views:
    681
  5. Roger Leigh
    Replies:
    8
    Views:
    420
    Karl Heinz Buchegger
    Nov 17, 2003
Loading...

Share This Page