|
-
Jan 19th, 2002, 02:15 PM
#1
Thread Starter
Hyperactive Member
char* passed to a function
PHP Code:
BOOL pmRetrieveComboText (HWND hWndCombo, char* dest)
{
UINT idx; //current index
UINT len; //length of index text
char* ret = '\0'; //the value to return
idx = SendMessage (hWndCombo, CB_GETCURSEL, NULL, NULL);
len = SendMessage (hWndCombo, CB_GETLBTEXTLEN, idx, NULL);
if ((idx == -1) || (len == -1)) return 0;
ret = new char[len + 1]; //allocate the necessary memory
//fill the allocated array with the item text
SendMessage (hWndCombo, CB_GETLBTEXT, idx, (LPARAM) ret);
dest = ret;
delete[] ret;
return TRUE;
}
why wont this function change dest? i basically wanna to pass a char* by reference, but it doesm't change anything...can anyone help?
thanks
Amon Ra
The Power of Learning.
-
Jan 19th, 2002, 02:38 PM
#2
transcendental analytic
dest as a pointer is passed by value, and anything passed (in fact all function parameters are always passed by value and will be free'd from the stack one the function exits, the only changes the function can do to external is data is by passing pointers to the data to be manipulated) to return a pointer as data you would need a pointer to a pointer to a char.
What you are intending to do is allocating a data within a function without guarrantee that it will be deallocated, this is symptoms of earlier experience with java or COM objects in VB.
What you should do is use a string instead or do the operations directly on char and let the calling function be responsible for having the memory allocated, or alternatively pass the length of the text gained in a separate function call
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 20th, 2002, 02:21 PM
#3
Code:
char *buf = *dest;
char *s = *ret;
while(s!=0x0) *buf++=*s++;
s=0x0;
delete[] ret;
-
Jan 20th, 2002, 02:33 PM
#4
transcendental analytic
*sigh*
Someone has been using C way too much
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|