0
\$\begingroup\$

I'm working with a PIC32MX564F128H in MPLABX 2.35 with pickit3. I'm trying to parse a message from UART. The character is received properly, but I'm stalling when I attempt to copy a character to a buffer. This is the part in my main loop:

                unsigned char*buffer="efg"; //dummy string
                GetDataBuffer(buffer, 3);

_

  UINT32 GetDataBuffer(unsigned char *buffer, UINT32 max_size)
    {
        UINT32 num_char;
        num_char = 0;

        while(num_char < max_size)
        {
            UINT8 character;
            while(!UARTReceivedDataIsAvailable(UART1));
            character = UARTGetDataByte(UART1);
            if(character == '\r')
                break;
            *buffer = character;  **\\PROGRAM STALLS HERE** 
            buffer++;
            num_char++;
        }
        return num_char;
    }

Symptoms:

In debug mode, after unsigned char pointer declaration: buffer address: 0xA0008010 Value: Invalid Address

After being passed into GetDataBuffer: buffer address: 0xA00071F8 Value: 0x9D005ED8 *buffer=0x65 ("e", at this point not written over.) Continuing, program stalls at point shown above until the debugger halts at an unknown location.

\$\endgroup\$
3
\$\begingroup\$

You are having this problem because you are attempting to modify buffer which you defined as pointer to char, or rather point to a string literal. You cannot modify this. You should use:

unsigned char buffer[]="efg";

instead. For more information, check out this post: Char array vs Char Pointer in C

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ So unsigned char*buffer="efg" is actually like a unsigned char const *const buffer="efg" where change in address pointed and value is not allowed?? \$\endgroup\$ – AlphaGoku Apr 18 '16 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.