What is the difference between these? Topic is solved

If you have a simple question and just want an answer.
DemoniacMilk
XCore Addict
Posts: 191
Joined: Tue Jul 05, 2016 2:19 pm

What is the difference between these?

Post by DemoniacMilk »

This is probably a question on C in general, but I found something that was kind of weird to me:

I have an array of a struct defined in thread A, storing the state of some clients.
I would like to modify the states in thread B, so I pass the adress of the array over a channel.

Thread A:

Code: Select all

sscClientStateRx_t sscClientStatesRx[n];
initClientStatesRx(sscClientStatesRx, n);
unsafe{
    sscToConfig <: (sscClientStateRx_t * unsafe) sscClientStatesRx;
}
Thread B:

Code: Select all

sscClientStateRx_t * unsafe clientStates;
unsafe{
    channelToManager :> clientStates;
}
I tried to access the client state struct members in thread B using

Code: Select all

case cfg_if[int i].setFilterIp(unsigned int uiRxIndex, unsigned int uiIp):
    unsafe{
        clientStates[uiRxIndex]->filterIp = uiIp;
    }
    break;
what ended in a compiler error "indirection requires pointer argument", so i tried

Code: Select all

case cfg_if[int i].setFilterIp(unsigned int uiRxIndex, unsigned int uiIp):
    unsafe{
        clientStates[uiRxIndex].filterIp = uiIp;
    }
    break;
what was compiled just fine.

another approach I saw being used somewhere else was

Code: Select all

case cfg_if[int i].setFilterIp(unsigned int uiRxIndex, unsigned int uiIp):
    unsafe{
        sscClientStateRx_t &clientState = clientStates[uiRxIndex];
        clientState.filterIp = uiIp;
    }
    break;
I had never seen anything like this third approach. What is the difference to the second approach? And why is the first approach not correct?
View Solution
peter
XCore Addict
Posts: 230
Joined: Wed Mar 10, 2010 12:46 pm

Post by peter »

Hi DemoniacMilk,

The compiler is right, this is just a pure C issue. You've got an array of structures. When you access one of the elements ( clientStates[uiRxIndex] ) what you have is a structure, so you need to use the dot operator.

The last bit:

Code: Select all

sscClientStateRx_t &clientState = clientStates[uiRxIndex];
is something that XC allows you to do which is from the world of C++, creating a reference to a structure. It is the equivalent of a pointer that you can't change, but you use it with the dot operator again.

Regards,

Peter