|
|
View previous topic :: View next topic |
Author |
Message |
armondo522 Guest
|
Passing strings to function? |
Posted: Thu Jan 15, 2009 9:58 am |
|
|
Hope someone cane help me out here. I am writting a program that will pass variable strings to a 4 line I2C LCD display. I would like to have 4 separate routines , one for each line and just pass the string to the designated line. My question is? How do I pass the string into the function, then loop around the string to send one charecter out at a time to the display. Below is a sample of my code. I think I am having a problem using pointers.
char str[20];
// String to pass into function
col=2;
char Header[17]={'M','S',' ','E','S','S',' ','C','o','n','t','r','o','l','l','e','r','0'};
Display_update_2( *header,col); // call to function
// Function
Void Display_update_2(*str, col)
{
int cntr;
char temp_str[20];
strcpy(*temp_str,*str);
//char *str;
/*
i2c_start () ; commented out so i can bypass I2C writes
i2c_write (disp_wrt) ;
i2c_write (71) ;
i2c_write (col); //col
i2c_write (2); //row
*/
while(*HEADER!= 0) ; // pointer method
// i2c_write (*HEADER) ;
*HEADER++;
//for(cntr=0;cntr<=strlen(&String_to_write);cntr++); // for/loop method
//{
//}
i2c_stop();
} |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Jan 15, 2009 10:35 am |
|
|
A few problems
The first is your sting is not a string but an array of chars, there is a difference. Your last char is '0' the character 0. A string is null terminated meaning the last char is a 0, not the character '0' but the actual value 0. Just remove the single quuotes around it.
You array is also 1 character short, it needs to be [18] for that paticualr string, must include the null terminating char.
The second is you use the pointer HEADER but this is not defined as such!
your routine should be
Code: |
I think this will cause a problem as it will created in rom and you can't pass it as a parameter
char header[18]={'M','S',' ','E','S','S',' ','C','o','n','t','r','o','l','l','e','r',0};
You could also use
char header[18] = "MS ESS Controller";
But I think you will have the same problem
try
char header[18];
strcpy(header, "MS ESS Controller");
// Just pass header as this is the address of the array
Display_update_2(header, col); // call to function
// Function (MISSING type info)
Void Display_update_2(char *str, int col)
{
int cntr;
// don't need this char temp_str[20];
// Don't need this strcpy(*temp_str,*str);
i2c_start () ; commented out so i can bypass I2C writes
i2c_write (disp_wrt) ;
i2c_write (71) ;
i2c_write (col); //col
i2c_write (2); //row
while(*str != 0) // loop until the char at str == 0
{
i2c_write(*str); // pass the char at str
str++; // increment the pointer str by 1
}
i2c_stop();
}
|
|
|
|
armondo522 Guest
|
String problem |
Posted: Thu Jan 15, 2009 11:17 am |
|
|
Thanks Wayne for the reply,
When I do this
Code: | char header[18]; // get error numeric expression must appear here
strcpy(header, "MS ESS Controller");
new function
Void Display_update_2( char str, int col)
{
int cntr;
/*
i2c_start () ;
i2c_write (disp_wrt) ;
i2c_write (71) ;
i2c_write (col); //col
i2c_write (2); //row
*/
cntr=strlen(*str); // this gives me 0 lenght
while(*str!= 0) ; // only cycles once and exits
// i2c_write (*str) ;
*str++;
i2c_stop();
} |
thanks in advance |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Jan 15, 2009 11:37 am |
|
|
I see some type confusion in your code, the below snippet compiles without errors, you may want to use it as a starting point:
Code: | Void Display_update_2( char *str, int col)
{
while(*str)
i2c_write (*str++) ;
}
void main(void)
{
char header[18];
strcpy(header, "MS ESS Controller");
Display_update_2( header, 0);
} |
|
|
|
armondo522 Guest
|
Thanks for the help... |
Posted: Thu Jan 15, 2009 1:40 pm |
|
|
Thanks for the help...works great.. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Jan 16, 2009 2:57 am |
|
|
You get the error
char header[18]; // get error numeric expression must appear here
most likely because all youe variable declarations need to be before any code is written within the scope of that var, so globals are declared before any function including main and locals are delared at the top of the function before any code!
I presume you already have some code before this line! |
|
|
|
|
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
|