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 support@ccsinfo.com

memcmp problem with bidimensional const array

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








memcmp problem with bidimensional const array
PostPosted: Wed Jul 25, 2007 1:08 pm     Reply with quote

if I use

unsigned char R[10][8] = .....
if (memcmp(&R[j][0],&buffer[1],8) == 0) ....

all is OK, but if I try

const unsigned char R[10][8] = .....
if (memcmp(&R[j][0],&buffer[1],8) == 0) ....

to use const array I have a compiler error. Why?

Best regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 25, 2007 1:40 pm     Reply with quote

It probably doesn't work because traditionally, in CCS, the "mem"
functions require both parameters to be RAM arrays.

For me to test it further, you need to:

1. Post the declaration statement for the 'buffer' array.

2. Finish the code. Show the initializations that are shown as "...".

3. Post your compiler version.

4. Post your PIC.

5. Post the error message that you're getting.
firefox78



Joined: 25 Jan 2005
Posts: 5

View user's profile Send private message

PostPosted: Thu Jul 26, 2007 3:02 pm     Reply with quote

My compiler version is "CCS PCH C Compiler, Version 4.013, 28193", pic is PIC18F4550 and the error is on the follow line
if (memcmp(&Richieste[j][0],&buffer[1],l1) == 0)
there is the error and is Expecting an identifire. All this errors are onli if I use CONST before array!!!! Why ? The full source code is the follow:

Code:

#include <18F4550>
#include <string.h>

#fuses INTRC_IO, NOWDT, NOBROWNOUT, PUT, NOLVP, NOMCLR, NOXINST
#use delay(clock=8000000)
#use RS232 (stream=uart, baud=9600, UART1, PARITY=N, BITS=8)

signed int16 l1 = -1;
signed int16 l2 = -1;
unsigned int16 t = 0;
unsigned char buffer[20];
int bancataricevuta = 0;

#define NRichieste 20
const unsigned char Richieste[NRichieste][8] =
                       {
                        {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
                        {0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
                        {0x01, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
                       };

const unsigned char Risposte[NRichieste][8] =
                       {
                        {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
                        {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02},
                        {0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03},
                        {0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04},
                       };


#int_TIMER2
void TIMER2_isr()
{
   t++;

   if ((t >= 100) && (l1>=0))
   {
     t = 0;
     bancataricevuta = 1;
   }
}

#INT_RDA
void irx()
{
     unsigned char x;
     while(kbhit(uart) == 1)
     {
        t = 0;
        l1++;
        x = getch(uart);
        buffer[l1] = x;
     }
}

void main()
{
   int16 i = 0;
   int16 j = 0;
   int8 trovato = 0;

   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_1,124,16);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);

   while(1)
   {
      if (bancataricevuta == 1)
      {
              trovato = 0;
              j = 0;
              while ((trovato == 0) && (j < NRichieste))
              {
                   if (memcmp(&Richieste[j][0],&buffer[1],l1) == 0)
                   {
                      l2++;
                      write_eeprom(l2,j);

                      trovato = 1;

                      output_high(PIN_B4);
                      delay_ms(10);
                      output_low(PIN_B4);

                      for (i=0;i<8; i++) putc(Risposte[j][i]);
                   }
                   j++;
              }

              if (trovato == 0)
              {
                 output_high(PIN_B7);
                 for (i=0; i<=l1; i++)
                 {
                    l2++;
                    write_eeprom(l2,buffer[i]);
                 }
                 delay_ms(10);
                 output_low(PIN_B7);
              }
           }

           l1 = -1;
           bancataricevuta = 0;
   }

}


I have only const array and I need for memory reasons that all array must be placed as CONST.

Best regards.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 27, 2007 5:40 pm     Reply with quote

You need to copy the 'const' array elements into a temporary RAM buffer
before you use the memcmp() function. To do this, make the changes
shown below.

Code:
const unsigned char Risposte[NRichieste][8] =
{
 {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
 {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02},
 {0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03},
 {0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04},
};

unsigned char tempbuf[8];    // ADD THIS LINE



Code:

while ((trovato == 0) && (j < NRichieste))
   {
    memcpy(tempbuf, Richieste[j], 8);         // ADD THIS LINE   
    if(memcmp(tempbuf, &buffer[1], l1) == 0)  // ADD THIS LINE

// COMMENT OUT THE FOLLOWING LINE:
//  if(memcmp(&Richieste[j][0], &buffer[1],l1) == 0)
       {
 
firefox78



Joined: 25 Jan 2005
Posts: 5

View user's profile Send private message

PostPosted: Sun Jul 29, 2007 1:33 pm     Reply with quote

PCM programmer wrote:
You need to copy the 'const' array elements into a temporary RAM buffer
before you use the memcmp() function. To do this, make the changes
shown below.

Code:
const unsigned char Risposte[NRichieste][8] =
{
 {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
 {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02},
 {0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03},
 {0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04},
};

unsigned char tempbuf[8];    // ADD THIS LINE



Code:

while ((trovato == 0) && (j < NRichieste))
   {
    memcpy(tempbuf, Richieste[j], 8);         // ADD THIS LINE   
    if(memcmp(tempbuf, &buffer[1], l1) == 0)  // ADD THIS LINE

// COMMENT OUT THE FOLLOWING LINE:
//  if(memcmp(&Richieste[j][0], &buffer[1],l1) == 0)
       {
 


OK Perfect, tankyou very much
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