This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430 and CCSv5 pointer issues.



I am trying to assign a buffer to a pointer through a function. Here is a simple code to that effect.

 

* main.c
 */
char buffer[256];
void getBuffer(char *outBuffer, int *outLength);

void main(void) {
    char *ptrToBuffer;
    int bufferLength;
    //Get the buffer defined above..
    getBuffer(ptrToBuffer,&bufferLength);
    __no_operation();

}
void getBuffer(char *outBuffer, int *outLength)
{
    //This statement never executes ????
    outBuffer = buffer;
    *outLength = 256;
}

The statement  "outBuffer = buffer" never executes. The compiler leaves it out since I do not see it in the disassembly.  I have a warning that says "#552-D parameter "outBuffer" was set but never used    main.c    /tiTest    line 15    C/C++ Problem"  but it shoud still assign the pointer with the address of the buffer.

Please advice.

Thanks and best Regards



  • The variable outBuffer exists only for the duration of the call to getBuffer(), and within that function it is assigned but never used.  Removing the assignment has no effect on the execution of the program, so the compiler removes it.

    Or are you expecting the assignment to affect the state of variables in main()?  That's not how the language works.

    I see that you have an outLength parameter, pointer-to-int, which you write through.  That will indeed set the variable bufferLength whose address you passed in the call.  To make the equivalent assignment to ptrToBuffer, you must pass &ptrToBuffer, define the parameter as "char **outBuffer", and write with "*outBuffer = buffer".