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

pointer to structure with strings confusion

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







pointer to structure with strings confusion
PostPosted: Wed Oct 25, 2006 2:51 pm     Reply with quote

hi,

i have an embedded database app running on an 18f8722 with 8M of flash. one of the data records is a structure which contains strings. the main function which saves this record to the data store passes the strings to a function which searches and stores them in an index (stored in FRAM). This function in turn performs string comparisons with existing values in the index.

I've stripped out most of the code from the functions, but left the calls to the index functions. i developed this app using Borland C++ builder using simulated flash and fram storage and all works well. however, i've now had a lot of problems porting to the ccs compiler due to syntax differences. in the code below you can see that i have had to change the function parameters for the ccs compiled version (i used printf() debugs in the index functions to print out the incoming strings and they were garbage until i added the pointer references).

i expected the "idxInsert(NewPatient->FamilyName,..)" to be passing an address of the string within the structure, which is what i was then dealing with in the function. this is how it works in the borland environment. on the pic target, i'm still getting unexpected behaviour even with the pointer parameters in that i can make the app work correctly if i add a printf() to display the string within the index function, but it fails if i don't ! and the more time i spend thinking about the less i understand... 'c' is not my first language so if someone can point out where i've tripped up i'd be most grateful.

ps. i've been testing this with 3.249 and various flavours of the v4 compiler.

Code:
typedef char tNameString[SizeofNameIndex];
typedef char tIDNumber[SizeofIDIndex];


struct tPatientRecord
  {
  tNameString FamilyName;
  tIDNumber IDNumber;
  etc
  };


__int8 dbAddPatient(struct tPatientRecord *NewPatient)
{
idxInsert(NewPatient->FamilyName, NewPatient->IDNumber, &Location);
}


#ifdef __WIN32__
__int8 idxInsert(tNameString NameKey, tIDNumber IDKey, struct tFlashAddress *Location)
#endif
#ifndef __WIN32__
__int8 idxInsert(tNameString *NameKey, tIDNumber *IDKey, struct tFlashAddress *Location)
#endif
{
if (idxFindMatch(NameKey, IDKey, &MatchLocation) == true) return false;
}


#ifdef __WIN32__
__int8 idxFindMatch(tNameString NameKey, tIDNumber IDKey, struct tFlashAddress *Location)
#endif
#ifndef __WIN32__
__int8 idxFindMatch(tNameString *NameKey, tIDNumber *IDKey, struct tFlashAddress *Location)
#endif
{
tNameString IdxKey;
crLoadBytes(IdxKey,(unsigned __int16)((IdxNum*SizeOfIDIndexRecord)+crStartOfIDIndex),SizeofIDIndex);
switch (idxCompareKey(IdxKey,IDKey,SizeofIDIndex))
  {
   //need to go up the list a bit further
  case -1:
etc
}


#ifdef __WIN32__
signed __int8 idxCompareKey(tKeyString ThisKey, tKeyString IdxKey, __int8 KeyLength)
#endif
#ifndef __WIN32__
signed __int8 idxCompareKey(tKeyString *ThisKey, tKeyString *IdxKey, __int8 KeyLength)
#endif
{
 //returns -1 for key less than idx, +1 for key greater than idx, and 0 for a match
signed __int8 i;
i = (signed __int8)strncmp(ThisKey,IdxKey,KeyLength);
return i;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 26, 2006 2:11 pm     Reply with quote

Quote:

I'm still getting unexpected behaviour even with the pointer parameters.

I found a limitation that may apply to your problem. When I was
experimenting with making arrays of structures, I found that the
compiler needs to be told the number of elements that are in the
array.

For example, in the code below, I had to put in the number '6', to
tell the compiler that the array has 6 structures in it. If I left it out,
sometimes the compiler would not accurately index into the structure
data in the array. It would not display the strings correctly. I used
PCM and PCH, vs. 3.249 for these tests.

Code:

typedef struct
{
char menu_type;
char line1[17];     
char line2[17];
}menu_t;

const menu_t menu_screens[6] =
{
{MAIN_MENU, "Setup PWM", ""},
{SUB_MENU, "Enable/Disable", ""},
{SUB_MENU, "Set PWM Freq", ""},
{SUB_MENU, "Set PWM Duty, ""},
{SUB_MENU, "More info:", ""},
{SUB_MENU, "Help", ""}
};
Guest








PostPosted: Fri Oct 27, 2006 3:10 am     Reply with quote

PCM, thanks for your thoughts. A couple of developments since my posting. I have changed the function parameters from the (tNameString..) to (char *ID) format. this has removed the need for my ifdefs to compensate for the behaviour difference between the CCS and borland compilers. It seems that CCS didn't cope with the typedefs of the strings when it is a pointer to a pointer (it's my pascal past that influences me - I like to "type" my parameters). By passing a pointer to the structure in the top level function, I can now pass a pointer to the string within the structure to the lower indexing functions.

However, I'm still being led a merry dance, and a single printf() in the indexing functions can completely change the behaviour of the indexing routines. I'm suspecting the problem is now deeper within the indexing routines, and is not related to the subject of this thread. I'll may be pop up again under a different thread title if I can tie it down to something describable! Thanks for your help.
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