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

Error "Attempt to create a pointer to a constant"

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



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

Error "Attempt to create a pointer to a constant"
PostPosted: Fri Jul 05, 2024 12:04 pm     Reply with quote

Compiler: v4.114

Error "Attempt to create a pointer to a constant".

Code:
// Function to check for "CAL" sequence
int check_CAL_sequence() {
    char buffer[4] = {0};  // Buffer to store received characters
    int index = 0;
   
    // Read characters until "CAL" sequence is detected or timeout occurs
    unsigned int16 timeout = 0;
    while (timeout < 5000) {
        if (kbhit()) {  // Check if character is available
            char received_char = fgetc();  // Read the character
           
            // Store character in buffer
            buffer[index++] = received_char;
            buffer[index] = '\0';  // Null-terminate the buffer
           
            // Check for "CAL" sequence
            if (strcmp(buffer, "CAL") == 0) {
                return 1;  // Return true if "CAL" sequence is detected
            }
           
            // Adjust buffer (shift left if buffer is full)
            if (index >= 3) {
                index = 0;  // Reset index if full (to prevent overflow)
                buffer[index] = '\0';  // Clear buffer
            }
        }
       
        delay_ms(1);  // Delay 1 millisecond
        timeout++;  // Increment timeout counter
    }
   
    return 0;  // "CAL" sequence not detected within timeout
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sat Jul 06, 2024 2:44 am     Reply with quote

A search here would find a lot about this.
You have a very old compiler. A method to do this was fixed in the modern
compilers. but the key is to understand the PIC, and read the manual.
In the PIC, the ROM, and RAM are two completely separate memory spaces.
So "CAL" as a constant, is stored in the ROM, and cannot be used inside
a string operation needing a RAM pointer. You can strcpy this value into
a RAM array to do the comparison.
If you read the manual section on strcmp, for your compiler, it says:
Quote:

s1 and s2 are pointers to an array of characters (or the name of an array). Note that s1 and s2 MAY NOT BE A CONSTANT (like "hi").


The note is critical.

Modern compilers can virtualise a constant like this by copying it into
a tiny temporary buffer. Uses a compiler setting 'PASS_STRINGS =
IN_RAM".

For your compiler, you have to do this yourself.
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