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

Passing a String in a function is it possible.

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



Joined: 20 Sep 2005
Posts: 27

View user's profile Send private message Visit poster's website

Passing a String in a function is it possible.
PostPosted: Fri Aug 31, 2018 11:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 31, 2018 11:32 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Fri Aug 31, 2018 12:16 pm     Reply with quote

That worked thank you for the help. Very Happy
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