Results 1 to 4 of 4

Thread: char* passed to a function

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    char* passed to a function

    PHP Code:
    BOOL pmRetrieveComboText (HWND hWndCombochardest)
    {
        
    UINT idx;        //current index
        
    UINT    len;    //length of index text
        
    charret '\0';        //the value to return

        
    idx SendMessage (hWndComboCB_GETCURSELNULLNULL);
        
    len SendMessage (hWndComboCB_GETLBTEXTLENidxNULL);

        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 (hWndComboCB_GETLBTEXTidx, (LPARAMret);
        
    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.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3
    jim mcnamara
    Guest
    Code:
        char *buf = *dest;
        char *s =    *ret;
        while(s!=0x0) *buf++=*s++;
        s=0x0;
    
        delete[] ret;

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    *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
  •  



Click Here to Expand Forum to Full Width