View previous topic :: View next topic |
Author |
Message |
David Pouzar
Joined: 20 Sep 2005 Posts: 27
|
Passing a String in a function is it possible. |
Posted: Fri Aug 31, 2018 11:21 am |
|
|
I am hoping there is a better way to rewrite this piece of code. It is from a vendor library.
Original Code (not modified)
Code: | /**
Writes a string of characters to the destination device.
This method must be used after the SPI interface has started.
@param data The data to be transmitted.
@return none
*/
void writeStringSPI(char* data){
for(int i = 0; i < arraySize(data); i++)
{
while(!(UCB0IFG & UCTXIFG)); // Check to see if transmit buffer is empty
UCB0TXBUF = data[i];
}
} |
Modified (does not compile)
Code: |
/**
Writes a string of characters to the destination device.
This method must be used after the SPI interface has started.
@param data The data to be transmitted.
@return none
*/
void writeStringSPI(char* data)
{
for(int i = 0; i < strlen(data); i++)
{
spi_xfer(SPI_STREAM,data[i],8);
}
} |
Error#90 Attempt to create a pointer to a constant.
I know I can do replace this piece of code with a prinf like below,
Code: |
printf(writeString2SPI,"INSTANT FLOW RATE");
void writeString2SPI(char b)
{
spi_xfer(SPI_STREAM,b,8);
}
|
I am hoping someone has a better idea for me. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19555
|
|
Posted: Fri Aug 31, 2018 11:32 am |
|
|
The problem is not in what you post. It is in where you call it.
You are presumably using something like:
WriteString("Something");
Problem at this point is that "Something", is a constant string (stored in ROM). This can't have a pointer constructed to it. Hence the warning.
Solutions:
1) Add #device PASS_STRINGS=IN_RAM
at the top of your code just after the processor include. This makes the compiler automatically copy constant strings into RAM and allow a pointer to be constructed to this, using a small RAM buffer.
2) strcpy the constant strings to RAM buffers, and pass this to the function. Does the same thing really, but you are doing it manually. |
|
|
David Pouzar
Joined: 20 Sep 2005 Posts: 27
|
|
Posted: Fri Aug 31, 2018 12:16 pm |
|
|
That worked thank you for the help. |
|
|
|