Problem using structure

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
cardedon
Posts: 5
Joined: 08 Nov 2010 15:49

Problem using structure

#1 Post by cardedon » 21 Aug 2012 19:43

Hi all,
I'm using a structure for my software (an homemade domotic system) for store all the temperature data

This is my struct definition

Code: Select all


struct TList {
  char listname[20];
  int temperature;
  int index;
  } TempList[20];

Then i try to write some dummy data for test the routines

Code: Select all


void dummytemps()
{
   TempList[0].listname = "Ambiente 1"; TempList[0].temperature = 25; TempList[0].index = 0;
   TempList[1].listname = "Ambiente 2"; TempList[1].temperature = 28; TempList[1].index = 1;
   TempList[2].listname = "Ambiente 3"; TempList[2].temperature = 30; TempList[2].index = 2;
   TempList[3].listname = "Ambiente 4"; TempList[3].temperature = 22; TempList[3].index = 3;
   TempList[4].listname = "Ambiente 5"; TempList[4].temperature = 20; TempList[4].index = 4;
   TempList[5].listname = "Ambiente 6"; TempList[5].temperature = 16; TempList[5].index = 5;
   TempList[6].listname = "Ambiente 7"; TempList[6].temperature = 18; TempList[6].index = 6;
   TempList[7].listname = "Ambiente 8"; TempList[7].temperature = 21; TempList[7].index = 7;
   TempList[8].listname = "Ambiente 9"; TempList[8].temperature = 23; TempList[8].index = 8;
   TempList[9].listname = "Ambiente 10"; TempList[9].temperature = 22; TempList[9].index = 9;
   TempList[10].listname = "Ambiente 11"; TempList[10].temperature = 19; TempList[10].index = 10;
   TempList[11].listname = "Ambiente 12"; TempList[11].temperature = 20; TempList[11].index = 11;
   TempList[12].listname = "Ambiente 13"; TempList[12].temperature = 21; TempList[12].index = 12;
   TempList[13].listname = "Ambiente 14"; TempList[13].temperature = 23; TempList[13].index = 13;
   TempList[14].listname = "Ambiente 15"; TempList[14].temperature = 16; TempList[14].index = 14;
}

And i receive this error

Code: Select all

Assigning to non-lvalue
Implicit conversion of pointer to int
Both errors are refered to the first assignment

Code: Select all

TempList[0].listname = "Ambiente 1"
Some idea for solve that issue?
Best regards
Andrea

Jonny
Posts: 38
Joined: 03 Jul 2007 03:49

Re: Problem using structure

#2 Post by Jonny » 21 Aug 2012 20:26

Hi,

Try using the strcpy function.

Code: Select all

strcpy(TempList[0].listname, "Ambiente 1");

cardedon
Posts: 5
Joined: 08 Nov 2010 15:49

Re: Problem using structure

#3 Post by cardedon » 21 Aug 2012 21:24

Ty so much it worked.
So strange that MikroC doesn't handle it automatically.
I found the same bug when i do an array of strings

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Problem using structure

#4 Post by janko.kaljevic » 22 Aug 2012 08:25

Hello,

mikroC is designed according to ANSI C standard.
And in this case compiler should through an error.

Jonny is right. The best and easiest would be to use string library for these operations.

Best regards.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: Problem using structure

#5 Post by Mince-n-Tatties » 22 Aug 2012 09:29

the error/warning is correct.

"x" is an array of characters which contains 1 character (and likely a null \0)

'x' is an intger used to produce the numeric value of the letter x

you can populate as follows

Code: Select all

TempList[0].listname[0] = 'A';TempList[0].temperature = 25; TempList[0].index = 0;
and

Code: Select all

TempList[0].listname[0] = 'A';
TempList[0].listname[1] = 'm';
TempList[0].listname[2] = 'b';
TempList[0].listname[3] = 'i';
TempList[0].listname[4] = 'e';
TempList[0].listname[5] = 'n';
TempList[0].listname[6] = 't';
TempList[0].listname[7] = 'e';
TempList[0].listname[8] = ' ';
TempList[0].listname[9] = '1';
This is the 8 bit embedded world, effiecient use of device resource has always been the key driver so for a compiler to covertly add the string library would not be expected. yes the string library function can of course be used but at the cost of RAM space.
Best Regards

Mince

Sy
Posts: 708
Joined: 10 Dec 2009 13:41
Location: UK

Re: Problem using structure

#6 Post by Sy » 22 Aug 2012 13:43

You could get the compiler to initialise the structure for you at compile time:

Code: Select all

struct TList {
  char* listname;
  int temperature;
  int index;
  } TempList[] = {
    { "Ambiente 1",  25, 0 },
    { "Ambiente 2",  28, 1 },
    { "Ambiente 3",  30, 2 },
    { "Ambiente 4",  22, 3 },
    { "Ambiente 5",  20, 4 },
    { "Ambiente 6",  16, 5 },
    { "Ambiente 7",  18, 6 },
    { "Ambiente 8",  21, 7 },
    { "Ambiente 9",  23, 8 },
    { "Ambiente 10", 22, 9 },
    { "Ambiente 11", 19, 10 },
    { "Ambiente 12", 20, 11 },
    { "Ambiente 13", 21, 12 },
    { "Ambiente 14", 23, 13 },
    { "Ambiente 15", 16, 14 }
  };
Note, the size of the array is now automatic according to how many initialisers are supplied. I've also removed the size of the array inside the structure, since they are used as pointers to a fixed string.
Last edited by Sy on 22 Aug 2012 18:54, edited 2 times in total.
Kind Regards,
Sy

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: Problem using structure

#7 Post by Mince-n-Tatties » 22 Aug 2012 15:52

for interest have a look and a play with this.

Code: Select all

// PIC P18F45K22
// 8Mhz
// using soft ICD for timing figures


struct TList {
  char listname[20];
  int temperature;
  unsigned short index;
  } TempList[20];



void TestMethod_3()
{
   strcpy(TempList[0].listname, "Ambient 1");TempList[0].temperature = 25; TempList[0].index = 0;
   strcpy(TempList[1].listname, "Ambient 2");TempList[1].temperature = 25; TempList[1].index = 1;
   strcpy(TempList[2].listname, "Ambient 3"); TempList[2].temperature = 30; TempList[2].index = 2;
   strcpy(TempList[3].listname, "Ambient 4"); TempList[3].temperature = 22; TempList[3].index = 3;
   strcpy(TempList[4].listname, "Ambient 5"); TempList[4].temperature = 20; TempList[4].index = 4;
   strcpy(TempList[5].listname, "Ambient 6"); TempList[5].temperature = 16; TempList[5].index = 5;
   strcpy(TempList[6].listname, "Ambient 7"); TempList[6].temperature = 18; TempList[6].index = 6;
   strcpy(TempList[7].listname, "Ambient 8"); TempList[7].temperature = 21; TempList[7].index = 7;
   strcpy(TempList[8].listname, "Ambient 9"); TempList[8].temperature = 23; TempList[8].index = 8;

}
void TestMethod_2()
{

      TempList[0].listname[0] = 'A';
      TempList[0].listname[1] = 'm';
      TempList[0].listname[2] = 'b';
      TempList[0].listname[3] = 'i';
      TempList[0].listname[4] = 'e';
      TempList[0].listname[5] = 'n';
      TempList[0].listname[6] = 't';
      TempList[0].listname[7] = ' ';
      TempList[0].listname[8] = '1';
      TempList[0].temperature = 25;
      TempList[0].index = 0;
      
      TempList[1].listname[0] = 'A';
      TempList[1].listname[1] = 'm';
      TempList[1].listname[2] = 'b';
      TempList[1].listname[3] = 'i';
      TempList[1].listname[4] = 'e';
      TempList[1].listname[5] = 'n';
      TempList[1].listname[6] = 't';
      TempList[1].listname[7] = ' ';
      TempList[1].listname[8] = '2';
      TempList[1].temperature = 25;
      TempList[1].index = 1;
      
      TempList[2].listname[0] = 'A';
      TempList[2].listname[1] = 'm';
      TempList[2].listname[2] = 'b';
      TempList[2].listname[3] = 'i';
      TempList[2].listname[4] = 'e';
      TempList[2].listname[5] = 'n';
      TempList[2].listname[6] = 't';
      TempList[2].listname[7] = ' ';
      TempList[2].listname[8] = '3';
      TempList[2].temperature = 30;
      TempList[2].index = 2;

      TempList[3].listname[0] = 'A';
      TempList[3].listname[1] = 'm';
      TempList[3].listname[2] = 'b';
      TempList[3].listname[3] = 'i';
      TempList[3].listname[4] = 'e';
      TempList[3].listname[5] = 'n';
      TempList[3].listname[6] = 't';
      TempList[3].listname[7] = ' ';
      TempList[3].listname[8] = '4';
      TempList[3].temperature = 22;
      TempList[3].index = 3;

      TempList[4].listname[0] = 'A';
      TempList[4].listname[1] = 'm';
      TempList[4].listname[2] = 'b';
      TempList[4].listname[3] = 'i';
      TempList[4].listname[4] = 'e';
      TempList[4].listname[5] = 'n';
      TempList[4].listname[6] = 't';
      TempList[4].listname[7] = ' ';
      TempList[4].listname[8] = '5';
      TempList[4].temperature = 20;
      TempList[4].index = 4;

      TempList[5].listname[0] = 'A';
      TempList[5].listname[1] = 'm';
      TempList[5].listname[2] = 'b';
      TempList[5].listname[3] = 'i';
      TempList[5].listname[4] = 'e';
      TempList[5].listname[5] = 'n';
      TempList[5].listname[6] = 't';
      TempList[5].listname[7] = ' ';
      TempList[5].listname[8] = '6';
      TempList[5].temperature = 16;
      TempList[5].index = 5;

      TempList[6].listname[0] = 'A';
      TempList[6].listname[1] = 'm';
      TempList[6].listname[2] = 'b';
      TempList[6].listname[3] = 'i';
      TempList[6].listname[4] = 'e';
      TempList[6].listname[5] = 'n';
      TempList[6].listname[6] = 't';
      TempList[6].listname[7] = ' ';
      TempList[6].listname[8] = '7';
      TempList[6].temperature = 18;
      TempList[6].index = 6;

      TempList[7].listname[0] = 'A';
      TempList[7].listname[1] = 'm';
      TempList[7].listname[2] = 'b';
      TempList[7].listname[3] = 'i';
      TempList[7].listname[4] = 'e';
      TempList[7].listname[5] = 'n';
      TempList[7].listname[6] = 't';
      TempList[7].listname[7] = ' ';
      TempList[7].listname[8] = '8';
      TempList[7].temperature = 21;
      TempList[7].index = 7;

      TempList[8].listname[0] = 'A';
      TempList[8].listname[1] = 'm';
      TempList[8].listname[2] = 'b';
      TempList[8].listname[3] = 'i';
      TempList[8].listname[4] = 'e';
      TempList[8].listname[5] = 'n';
      TempList[8].listname[6] = 't';
      TempList[8].listname[7] = ' ';
      TempList[8].listname[8] = '9';
      TempList[8].temperature = 23;
      TempList[8].index = 8;

}


void TestMethod_1()
{

  char *loc;
  short a = 0;

   for (a=0;a<=8;a++)
     {
      loc = TempList[a].listname;
     
      *loc++ = 'A';
      *loc++ = 'm';
      *loc++ = 'b';
      *loc++ = 'i';
      *loc++ = 'e';
      *loc++ = 'n';
      *loc++ = 't';
      *loc++ = ' ';
      *loc++ = '0'+ (a+1);
      TempList[a].index = a;
     }

    TempList[0].temperature = 25;
    TempList[1].temperature = 25;
    TempList[2].temperature = 30;
    TempList[3].temperature = 22;
    TempList[4].temperature = 20;
    TempList[5].temperature = 16;
    TempList[6].temperature = 18;
    TempList[7].temperature = 21;
    TempList[8].temperature = 23;

}

void main() {

delay_us(1); // for breakpoint usage

//TestMethod_1();   // takes 982.00us  to execute program up to second delay_us(1)
                    // Used RAM (bytes): 463 (31%)  Free RAM (bytes): 1052 (69%)
                    // Used ROM (bytes): 472 (1%)  Free ROM (bytes): 32296 (99%)
                    // acceptable amount of typing but Middle in RAM usage Best in ROM usage

TestMethod_2(); // takes 112.00us to execute program up to second delay_us(1)
                  // Used RAM (bytes): 460 (30%)  Free RAM (bytes): 1055 (70%)
                  // Used ROM (bytes): 490 (1%)  Free ROM (bytes): 32278 (99%)
                  // a lot of typing to implement but Best in RAM usage Middle in ROM usage

//TestMethod_3();   // takes 1.85ms  to execute program up to second delay_us(1)
                  // Used RAM (bytes): 554 (37%)  Free RAM (bytes): 961 (63%)
                  // Used ROM (bytes): 564 (2%)  Free ROM (bytes): 32204 (98%)
                  // least amount of typing but Highest RAM usage Highest ROM usage
                  
// TestMethod_4 // was the same as TestMethod_1 using a For loop with individual 
                // struc statements similar to my previous post.
                // i took this out as the testmethod_1 using pointers was 4 times faster
                // with about the same amount of typing for the human!!


delay_us(1); // for breakpoint usage for program execution time measure
             // set run to cursor here
}
Best Regards

Mince

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: Problem using structure

#8 Post by Mince-n-Tatties » 23 Aug 2012 12:03

wow try testmethod_3 with the compiler set for Dynamic link for string literals

look at the ROM usage 312bytes extra used ouch...

Used RAM (bytes): 554 (37%) Free RAM (bytes): 961 (63%)
Used ROM (bytes): 876 (3%) Free ROM (bytes): 31892 (97%)
Best Regards

Mince

Sy
Posts: 708
Joined: 10 Dec 2009 13:41
Location: UK

Re: Problem using structure

#9 Post by Sy » 23 Aug 2012 12:32

Hi Mince, any reason why the method I suggested shouldn't be faster ?

Thanks,
Kind Regards,
Sy

Post Reply

Return to “mikroC PRO for PIC General”