Re: unable to use a local variable
> its also horrible programming practice to do it the way you have. > its best to declare all the variables you need at the top, and, > give them values below. in rare cases, I don't think it is a *horrible* programming practice to declare your variables where you use them (assuming you are using C++ ofcourse). If I had a function that was 150 lines long, and I need a local variable near the end, why would I declare it at the top of the function (or block)? That would require the programmer that comes in after me to remember how the variable was declared at the top of the block, and if he forgot, he would have to scroll back up to see how it was declared. One other point is that it keeps another programmer from coming in and using the variable assuming that it is already setup correctly. For example. void test( void ) { // Non meaningfule value, just an initiailizer. int a = 0; // Maintnance programmer inserts line here. a += 20; // Wants 120, but gets 20. // Intial meaningful value a = 100; } The code snippet is meaningless in itself, but the scenario can happen happen. If *a* were declared where it was used, the compiler would throw out an error long before a bug was introduced. So is it so horrible? Brad -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: unable to use a local variable
Blackwell Chris A Maj CENTAF-PSAB/C3 TCT Attack Coordinator <[EMAIL PROTECTED]> wrote: > > Hate to ask this "stupid" question but my sanity has overcome my ego... > > I am working with the Code Warrior Palm OS SDK 4.0. > > I've stuck mostly with the loaded template and started to add on a few > proceedures. I'm trying to use a local variable and its causing an > "Expression Syntax Error" on the next variable declaration. Ie: > > void DBManualEntry() > { >Err er; >UInt16 position=13; >//position+=11; > >MemHandle vh=MemHandleNew(3); >MemPtr npos = MemHandleLock(vh); > >ListBoxStrType *payments = MemPtrNew(sizeof(ListBoxStrType)); > > > } > > If I let "position+=11;" compile, I will get an Expression Syntax Error on > the next line "MemHandle vh..." > > I've been teaching myself with a book and I just keep missing this part. > Huge thanks for any help. > First comment: get a better book to teach yourself C. In C any executable statements (like your position += 11) have to follow the local variable declarations in your function (here DBManualEntry). You are confused by the fact that declaration statements can have initialization clauses. In fact all the statements you quote are declarations (local variables: er, vh, position, npos, payments). So the 'position+=11;' statement should be just before (or after) the . Good luck with teaching yourself C. PalmOS programming is yet an other kettle of fish ... Ton van Overbeek -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: unable to use a local variable
> I am working with the Code Warrior Palm OS SDK 4.0. probably best not to mention it like that, you normally have a release of codewarrior (r5,6,7,8 or 9) and, the SDK release. the SDK release can be used in any codewarrior version by default. the codewarrior compiler version is probably more important than the SDK version :) > I've stuck mostly with the loaded template and started to add on a few > proceedures. I'm trying to use a local variable and its causing an > "Expression Syntax Error" on the next variable declaration. Ie: > > void DBManualEntry() > { > Err er; > UInt16 position=13; > //position+=11; > > MemHandle vh=MemHandleNew(3); > MemPtr npos = MemHandleLock(vh); yes, thats standard C rules. you are not allowed to create a new declaration for a variable once you have started writing statements that operate on declarations. its also horrible programming practice to do it the way you have. its best to declare all the variables you need at the top, and, give them values below. in rare cases, you may be tempted to use a code block for counter variables etc, or variables more specific to a branch of the code, ie: x += 13; { MemHandle y; y = ...; } you can check this up in more detail by looking at the 1990 ISO C standard, which specifically tells you: "... all variables be declared at the start of a scope block." > I've been teaching myself with a book and I just keep missing this part. > Huge thanks for any help. start of here: http://www.lysator.liu.se/c/bwk-tutor.html never hurts to go over your C again :) and Kernighan is always a good start when it comes to catching up on your C :P --- Aaron Ardiri [EMAIL PROTECTED] CEO - CTO +46 70 656 1143 Mobile Wizardry http://www.mobilewizardry.com/ -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: unable to use a local variable
> void DBManualEntry() > { > Err er; > UInt16 position=13; > //position+=11; > > MemHandle vh=MemHandleNew(3); > MemPtr npos = MemHandleLock(vh); > > ListBoxStrType *payments = MemPtrNew(sizeof(ListBoxStrType)); > > > } > > If I let "position+=11;" compile, I will get an Expression Syntax > Error on the next line "MemHandle vh..." Just a guess, but are you compiling a C++ file, or straight C? C++ will let you get away with what you're doing there, but C won't. C needs to see all declarations of variables before any executable code in a given function. The following should compile even under a C compiler: void DBManualEntry() { Err er; UInt16 position=13; MemHandle vh; MemPtr npos; ListBoxStrType *payments; position+=11; vh=MemHandleNew(3); npos = MemHandleLock(vh); payments = MemPtrNew(sizeof(ListBoxStrType)); } -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
unable to use a local variable
Hate to ask this "stupid" question but my sanity has overcome my ego... I am working with the Code Warrior Palm OS SDK 4.0. I've stuck mostly with the loaded template and started to add on a few proceedures. I'm trying to use a local variable and its causing an "Expression Syntax Error" on the next variable declaration. Ie: void DBManualEntry() { Err er; UInt16 position=13; //position+=11; MemHandle vh=MemHandleNew(3); MemPtr npos = MemHandleLock(vh); ListBoxStrType *payments = MemPtrNew(sizeof(ListBoxStrType)); } If I let "position+=11;" compile, I will get an Expression Syntax Error on the next line "MemHandle vh..." I've been teaching myself with a book and I just keep missing this part. Huge thanks for any help. Chris -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/