This is a basic C programming question. Let me put on my CS101 hat and take a swing at it.
char name[1]; is completely different from CharPtr name; and char* name. The later two are the same (assuming that CharPtr is typedef'd as char *, which earlier versions of Palm header files did). char name[1] reserves a single byte of storage on the stack whose address is symbolically known as name. The compiler knows that name refers to an address on the stack and will resolve it, normally as the value of the stack pointer plus/minus some constant offset. You can think of name as being a constant pointer, but it really is not. You absolutely may not use name as the left side of an assignment statement (it is not an "lvalue"). char* name reserves four (in Palm OS) bytes of storage on the stack, which the compiler will know is a pointer to data of type char. It reserves absolutely no storage for the characters themselves, just for an address that is supposed to point to some characters. You absolutely may, and normally do, use name as an lvalue -- by far the most common way to make it point to real memory is a statement such as: name = MemPtrNew(1); which causes memory to be allocated at run time (on the heap) and the address of that memory to be stored in the pointer variable name. Hope that makes it a bit clearer. -bob mckenzie palm portland -----Original Message----- From: David Beers [mailto:[EMAIL PROTECTED]] Sent: Tuesday, January 29, 2002 1:24 AM To: Palm Developer Forum Subject: Pointers, arrays, and DmStrCopy A member of my Palm developers group in Houston asked a question on our board that we haven't been able to answer. None of us is very versed in C or the OS (yet!) so I guess I shouldn't be surprised! Here's the question: Hi everyone, I'm trying to learn palm programming on codewarrior and have hit on a problem. char name[1] CharPtr name; char *name; I thought these 3 things were the same thing. But when I use DmStrCopy to populate a field in the database with one of these types only char name[1] seems to work. The rest seem to result in garbage in the field. ****** I lamely suggested that the problem might have to do with the fact that pointers and arrays aren't /exactly/ the same thing, but I couldn't explain why referring to an array seemed to work but referring to a pointer did not. His answer expresses our mutual confusion: AS I understood it, when you define an array - char name[1]; name on its own is pointer reference to the first element of the array. i.e char *y = name; points to name[0]. So what I don't get is if name translates to a charptr why does DmStrCopy(s, offset, UserRecord->name) work ok, but when you actually replace that with something defined as a charptr it doesn't work. ****** I tried to find an answer in the KB, but no luck. Thanks in advance for taking the time to help a bunch of newbies! -- Sent using One-Touch Mail by JP Mobile. -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/ -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
