View previous topic :: View next topic |
Author |
Message |
Seijling
Joined: 06 Feb 2008 Posts: 3
|
Variable Declaration - Position Dependant? - SOLVED |
Posted: Wed Feb 06, 2008 10:47 pm |
|
|
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
|
|
Posted: Wed Feb 06, 2008 11:01 pm |
|
|
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
|
|
Posted: Wed Feb 06, 2008 11:14 pm |
|
|
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.
Michael K
Compiler 4.038
Device 16F877A |
|
|
Seijling
Joined: 06 Feb 2008 Posts: 3
|
|
Posted: Thu Feb 07, 2008 12:13 am |
|
|
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.
The solution was basically this:
Code: | #device PASS_STRINGS=IN_RAM |
Michael |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Feb 07, 2008 6:14 am |
|
|
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
|
|
Posted: Thu Feb 07, 2008 12:28 pm |
|
|
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.
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 |
Posted: Thu Mar 13, 2008 3:09 am |
|
|
Great topic )))))))))))))
WBR,
Alex |
|
|
|