CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Struct or array problem?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ljmnunes



Joined: 02 Sep 2004
Posts: 13
Location: Brazil

View user's profile Send private message

Struct or array problem?
PostPosted: Thu Jan 15, 2015 8:01 am     Reply with quote

Hi!

I have a problem to transfer one value from an array of bits to a member of an array of struct. I don't understand why the compiler don't transfer the value from the matrix to the struct directly.

The compiler is Version 5.036. Any ideas?

Code:

#include <18F46K22.h>
#include globals.c
#include tscc.c

main()
{
...
    while(1){
        if(x==2 || x== 4)
            TransferToTable();
        ...
       
   }
}

inside globals.c:
Code:

struct VirtualOut{
        unsigned int8  Fcod;
        unsigned int16 VAL;
}VirtualOut[NUM_VOUT];

int1 VarDig[512];

inside tscc.c:
Code:

void TransferToTable(void)
{
    unsigned INT16 Add;
    unsigned INT8  Ind;
    Add = i2c_readword();
    Ind = i2c_readbyte();
    VirtualOut[Ind].Val = Vardig[Ind]; // This line don't work!
}

After the changes below VirtualOut[ Ind].Val has the expected values​:
Code:

    int1 x = Vardig[Add];
    VirtualOut[Ind].Val = x;
 

Where is my fail?

Thanks a lot!
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jan 15, 2015 8:51 am     Reply with quote

You NEED to post a complete,small program NOT 'bits and pieces' It's too hard to follow.
I'd get rid of the includes....
Also you have an I2C...... lines implying you're reading from an I2C device ? We don't know if your drivers are correct, hardware works properly, or what you expect to happen...

More details are needed especially a small,complete 'copy/paste' program so others can try it.

hth
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19510

View user's profile Send private message

PostPosted: Thu Jan 15, 2015 9:23 am     Reply with quote

However as a comment, try one thing. Cast the int1, to an int1, when you read it.
I remember there being problems in the past, with int1's. The default type for all operations is int8. However without a maths operation involved, the cast doesn't take place, giving the wrong result. So try:
Code:

    VirtualOut[Ind].Val = (int1)Vardig[Ind];


I've said before that anything that involves 'using' an int1, in an unexpected place, should explicitly cast it before use!.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Jan 15, 2015 10:01 am     Reply with quote

CCS C does not support arrays of bits. This is from the manual:

Quote:
Arrays of bits are not permitted

Arrays may not be of SHORT INT. Arrays of Records are permitted but the record size is always rounded up to the next byte boundary.
Ttelmah



Joined: 11 Mar 2010
Posts: 19510

View user's profile Send private message

PostPosted: Thu Jan 15, 2015 2:47 pm     Reply with quote

Sorry RF_Developer, you are out of date.
From the current manual:

"Arrays of bits (INT1 or SHORT ) in RAM are now supported."

However they do have some oddities. Hence my suggestion about the cast. Problem is that they don't automatically convert when passed to other types.
ljmnunes



Joined: 02 Sep 2004
Posts: 13
Location: Brazil

View user's profile Send private message

PostPosted: Fri Jan 16, 2015 7:24 am     Reply with quote

Ttelmah wrote:
However as a comment, try one thing. Cast the int1, to an int1, when you read it.
I remember there being problems in the past, with int1's. The default type for all operations is int8. However without a maths operation involved, the cast doesn't take place, giving the wrong result. So try:
Code:

    VirtualOut[Ind].Val = (int1)Vardig[Ind];


I tryed do casting with int1 and it don't work.


temtronic wrote:
I'd get rid of the includes....

I can't do that. Here is my main #include list:
Code:
#include    "vars/globals.c"
#include    "func/prototypes.c"
#include    "drivers/DS1307.C"
#include    "drivers/AT24C512.C"
#include    "memory/mem.c"
#include    "serial/serial.c"
#include    "rtc/agenda.c"
#include    "esidio/common.c"
#include    "crc/crc.c"
#include    "modbus/ModbusSlave.c"
#include    "modbus/Tscc.c"
#include    "codi/codi.c"
#include    "io/perifericos.c"
#include    "io/io.c"
#include    "memory/tabelas.c"
#include    "io/interrupts.c"


There are tens of thousands of code lines to manage. As you can see, my project is too big to put the code here. I think that the snippets that I posted are sufficient and has all information about the problem. I have many others functions getting data of i2c line and are working fine. Sorry by wasting your time. I'll take care next time.

I also posted the solution that I have found. This shows that rest of the program is working. Whenever I post doubts I take care to eliminate all factor that don't care about the problem.

In this case the problem is only about transfer value form an array of bits and a struct. My solution is not elegant, I know. This is because I posted my doubt.

Any ideas will be welcome.

Anyway thanks Everyone for your help. We learn a lot here!

If I found other solution I'll update the post.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group