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 support@ccsinfo.com

Variable Declaration - Position Dependant? - SOLVED

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



Joined: 06 Feb 2008
Posts: 3

View user's profile Send private message MSN Messenger ICQ Number

Variable Declaration - Position Dependant? - SOLVED
PostPosted: Wed Feb 06, 2008 10:47 pm     Reply with quote

This may appear the most newb question possible but: can one declare variables at any point in TODO code?

i.e.

Code:

...
char *MakeString(char string[])
{
   int len;
   len = strlen(string);
   
   char temp[len]; //this declaration
}
...


The reason I ask is because if I try this "maneuver" I get the error message:
Quote:
A numeric expression must appear here
which points to the line where the char is declared.

You can see why I am declaring it later in the code since work must be done first to obtain the char array's length.

Any thoughts would be appreciated.

Michael K

compiler 4.038
device 16F877A


Last edited by Seijling on Thu Feb 07, 2008 12:13 am; edited 2 times in total
Guest








PostPosted: Wed Feb 06, 2008 11:01 pm     Reply with quote

CCS C is like 'Small C' it is not C++. From a C manual I found on the web,

Local variables
Local variables must always be defined at the top of a block.

You must allocate the max length of the string - later you can pass pointers around.

HTH - Steve H.
Seijling



Joined: 06 Feb 2008
Posts: 3

View user's profile Send private message MSN Messenger ICQ Number

PostPosted: Wed Feb 06, 2008 11:14 pm     Reply with quote

There lies the rub - the length of the string will be variable.

I know I could just pick an upper limit on the length for the string. The problem is that I will pass the string (and only the string) to a function which "pastes" it into a buffer which is a predefined 40 chars wide.

Once the data has been placed, I send the buffer -and yes, the premise for doing this is for an LCD application.

The function for doing this is:

Code:
void MakeBuffer (char *string, int LocX, int LocY)
{
   int i;
   switch (LocY)
      {
      case 1:
         for(i=0;i<strlen(string);i++) Line1[LocX+i-1] = string[i];
         break;
      case 2:
         for(i=0;i<strlen(string);i++) Line2[LocX+i-1] = string[i];
         break;
      default:
         errorLED();
      }
}
}


You can see that if I pass a string that is padded with more than the required data, I may overwrite buffer data.

Something tells me I am doing this in the most complicated way possible. Rolling Eyes

Michael K

Compiler 4.038
Device 16F877A
Seijling



Joined: 06 Feb 2008
Posts: 3

View user's profile Send private message MSN Messenger ICQ Number

PostPosted: Thu Feb 07, 2008 12:13 am     Reply with quote

Hey! What do you know? I actually found something on the CCS webby that covers this:

http://www.ccsinfo.com/content.php?page=flexconst

Very useful and informative (as always). Way to go CCS. Very Happy

The solution was basically this:

Code:
#device PASS_STRINGS=IN_RAM


Michael
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Feb 07, 2008 6:14 am     Reply with quote

To do what you want in your first post you need to either have a MAX string size or dynamically allocate and DO NOT FORGET to de-allocate memory:-

Code:

void myfun(char *string, len) {
  char *buf;
  buf = malloc(len + 1); // Enough space for the null terminating char
  strcpy(buf, string);

  // do something with buf

  free(buf); // If you DON'T do this you could eat up memory. This can be done elsewhere in your code.
}


BUT to do what you are doing with the buffer you just need some protection :-

Code:


void MakeBuffer (char *string, int LocX, int LocY)
{
   int i;
   switch (LocY)
      {
      case 1:
         for(i=0;i<strlen(string);i++) {
           Line1[LocX+i-1] = string[i];
           if (LocX + i >= 40)
              break;
         }
         break;
      case 2:
         for(i=0;i<strlen(string);i++) {
            Line2[LocX+i-1] = string[i];
            if (LocX + i >= 40)
               break;
         }
         break;
      default:
         errorLED();
      }
}
}

Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Thu Feb 07, 2008 12:28 pm     Reply with quote

Seijling wrote:
There lies the rub - the length of the string will be variable.
You can see that if I pass a string that is padded with more than the required data, I may overwrite buffer data.

Something tells me I am doing this in the most complicated way possible. Rolling Eyes

Michael K

Compiler 4.038
Device 16F877A


Code:
#include <string.h>
   Int8 LCD_buffer1[41];
   Int8 LCD_buffer2[41];

void main()
{  strcpy (LCD_buffer1[4], "Hi There");
   strcpy (LCD_buffer1[0], "Hi There");
}


This may be a better way for you to get what you want. Simply include the index to place the text where you want it to go. This is also easy to read.
luckylucker
Guest







great topic
PostPosted: Thu Mar 13, 2008 3:09 am     Reply with quote

Great topic )))))))))))))
WBR,
Alex
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