|
|
View previous topic :: View next topic |
Author |
Message |
John A.D.M Guest
|
Strcpy String to Structure Fails |
Posted: Tue Feb 02, 2010 4:30 pm |
|
|
Good Evening,
I'm writing a small program which acts a bit like a DOS environment on a PIC over RS232, however I'm encountering a very strange problem. I usually never ask for help but this time I cant figure why it does not work!!
Heres the code...
Code: |
//************************
/*HEADER DEFINITIONS AND ENVIRONMENT VARIABLES*/
#include <16F648A.h>
#device PASS_STRINGS=IN_RAM // Work around for getting pointers to access constant strings
#fuses NOLVP, NOMCLR, INTRC, NOWDT, NOPROTECT, NOCPD
#use delay(clock=4000000) // 4MHz
#include <STRING.h>
#define max_label_len 11 // max name length for a command; 10 + 1(for null character)
#define cmd_len 5 // total count of commands
typedef void (*_Func)(void); // Define a function pointer as type: not supported otherwise
//************************
// INTERNAL COMMAND DATABASE
struct cmd_entry
{
char name[max_label_len]; // represents the literal mnemonic of the action/verb
_Func func; // pointer to function, _Func => void (*_Func)(void)
} cmd_db[cmd_len]; // structure stored in array
int command_counter=0;
void test(void)
{}
// assign command to command database
void CmdLoader(char* funcname,_Func funcptr)
{
// ERROR LINE BELOW, COMPILER WONT COPY TO STRUCTURE!
strcpy(cmd_db[command_counter].name,funcname); // copy function name into structure
cmd_db[command_counter].func=funcptr; // pointer to function is stored
command_counter++;
}
void main(void)
{
char usin[max_label_len]="test"; // "us-in" stands for user input
CmdLoader(usin,test);
}
|
I have simulated the code often and pinpointed the problem down to a single line.
Why can't strcpy copy the string to the structure?
It works if I use
Code: | strcpy(cmd_db[command_counter].name,"test"); |
instead.
Thanks in advance. Whoever solves it gets a cookie... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 02, 2010 5:38 pm |
|
|
It would help if you would strip it down to a test program with only a
few lines of code. Get rid of all the comments and the function pointer
stuff, since that's not related to the strcpy problem. Use shorter variable
names if possible. Make it easy for us to look at the problem.
Post your compiler version. |
|
|
John A.D.M. Guest
|
|
Posted: Wed Feb 03, 2010 3:37 am |
|
|
Okay, fair point. I use MPLAB IDE 8.20 in combination with CCS PCB PCM PCH 4.087.
Code: |
#include <16F648A.h>
#fuses NOLVP, NOMCLR, INTRC, NOWDT, NOPROTECT, NOCPD
#use delay(clock=4000000) // 4MHz
#include <STRING.h>
struct cmd_entry
{
char name[11];
} cmd_db[5]; // structure stored as array
int command_counter=0;
void CmdLoader(char* funcname)
{
// ERROR LINE BELOW, COMPILER WONT COPY TO STRUCTURE!
strcpy(cmd_db[command_counter].name,funcname); // copy function name into structure
command_counter++;
}
void main(void)
{
char string[11]="test";
CmdLoader(string);
}
|
I programmed it on a PIC and used printf statements to show whats allocated. For some reason it wont copy a string stored under a variable into the structure...
Oh and thanks for your time. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 03, 2010 2:17 pm |
|
|
I agree there is a problem. It's possible that a work-around can be found.
I'll look at it more, later today. |
|
|
John A.D.M Guest
|
|
Posted: Wed Feb 03, 2010 3:34 pm |
|
|
At one point I had the thought it could be that strings are stored in the ROM, correct me if im false, and could not be accessed via pointer.
So I used #Device Pass_strings = In_Ram as I read in another post, but without success...
The significant part is that it works as "example" but not as a pointer. Btw, i checked whether the string is parsed to the function correctly and used it to copy from one string to another, no problem... its only structures. |
|
|
John A.D.M. Guest
|
|
Posted: Thu Feb 04, 2010 12:05 pm |
|
|
Any progress, any opinions.... im meant to demonstrate this on monday to investors :S |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 04, 2010 4:44 pm |
|
|
I was able to make it work by using strncpy() instead of strcpy().
I tested the program below with vs. 4.087 in MPLAB simulator, and it
gave the following output:
Quote: |
String = test
String = ABC
String = DEF
String = 123456
String = Done
|
That's correct. Use strncpy() as shown below as a work-around.
Code: |
#include <16F648A.H>
#device *=16
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, ERRORS)
#include <STRING.h>
struct cmd_entry
{
char name[11];
}cmd_db[5];
int command_counter;
void CmdLoader(char* funcname)
{
strncpy(cmd_db[command_counter].name, funcname, strlen(funcname));
command_counter++;
}
//=====================================
void main(void)
{
int8 i;
char string1[11]="test";
char string2[11]="ABC";
char string3[11]="DEF";
char string4[11]="123456";
char string5[11]="Done";
command_counter = 0;
// Load strings into cmd_entry structure.
CmdLoader(string1);
CmdLoader(string2);
CmdLoader(string3);
CmdLoader(string4);
CmdLoader(string5);
// Now display the structure contents.
for(i = 0; i < 5; i++)
printf("String = %s \r", cmd_db[i].name);
while(1);
} |
|
|
|
John A.D.M Guest
|
|
Posted: Thu Feb 04, 2010 5:36 pm |
|
|
Great Thanks.
I really appreciate your time.
Thats one strange bug though.... |
|
|
|
|
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
|