CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2003
    Location
    Australia
    Posts
    30

    LPVOID to char *

    I have some code that looks like this:

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int Foo(LPVOID Bar)
    {
        Bar = "Hello";
        cout << "Bar: " <<  Bar << endl;
        return 0;
    }
    
    int main()
    {
        char * Word = "bye";
        Foo(&Word);
    
        cout << "Word: " << Word << endl; 
        return 0;
    }
    When I run it I get the output:
    Code:
    Bar: hello
    Word: bye
    When Word should also be set to 'hello'. Can anyone tell me whats gone wrong? I'm useless when it comes to pointers, I don't see why the value of Word isn't being set to 'hello'
    Last edited by stovellp; September 30th, 2003 at 09:13 PM.
    SWEBS Web Server - http://swebs.sourceforge.net

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Strange code example... Don't know what you're trying to do here, but the statement
    Code:
    Bar = "Hello";
    will just assign a new value to the local copy of the char* variable Bar in foo(). It will not modify the memory pointed to by 'Word'. It makes no difference if you pass '&Word' instead of 'Word', as 'Word' is already a pointer. So it would be helpful if you could explain what you are actually trying to do.

  3. #3
    Join Date
    Sep 2003
    Location
    Australia
    Posts
    30
    Well, I'm trying to use the 'Foo' function to set the value of main()'s "Word" variable.

    This is what I got with a bit more experimentation:
    Code:
    int Foo(LPVOID Bar)
    {
        strcpy ((char *)Bar, "Hello\0");
        cout << "Bar: " << (char*)Bar << "    Address: " << Bar << endl;
        return 0;
    }
    
    int main()
    {
        char Word[10] = "Bye";
        Foo(&Word);
    
        cout << "Word: " << Word << "    Address: " << &Word << endl; 
        return 0;
    }
    This works perfectly, both Word and Bar are set to "Hello", and the addresses printed are the same.

    Now this is the part I have problems with. When I change:
    char Word[10] = "Bye";
    into
    char * Word = "Bye";

    There is illegal operations all over the place. This is simply a dumbed down version of a function that may be called with char []'s and char *'s, so I have no idea what will be passed, so I need to be able to make sure both work. Can you help me?
    SWEBS Web Server - http://swebs.sourceforge.net

  4. #4
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    When I run that program, I get:
    Bar: 0046E024
    Word: bye
    not
    Bar: hello
    Word: bye

    At any rate...
    Try this if you want to modify "Word":
    PHP Code:
    #include <iostream>
    #include <windows.h>

    using namespace std;

    int Foo(LPVOIDBar)
    {
        *
    Bar "Hello";
        
    cout << "Bar: " <<  Bar << endl;
        return 
    0;
    }

    int main()
    {
        
    char Word "bye";
        
    Foo((LPVOID*)&Word);

        
    cout << "Word: " << Word << endl
        return 
    0;

    Of course, this is not good practice (in either case). What prompted your initial post? Curiosity? Or some deeper issue? What are you trying to do?
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  5. #5
    Join Date
    Sep 2003
    Location
    Australia
    Posts
    30
    Well, the 'Foo' function is actually one loaded from a DLL. That means I cannot change "LPVOID Bar" to "LPVOID * Bar". Plus, I thought LPVOID is just a long void *?

    Without changing the function declaration I need to be able to change the value of Word (the function is for an ISAPI DLL that has to set some server variables if your wondering).
    SWEBS Web Server - http://swebs.sourceforge.net

  6. #6
    Join Date
    Sep 2003
    Location
    Australia
    Posts
    30
    vicodin, when I run your code I get:

    Bar: 0012FF7C
    Word: Hello

    I'm using MSVC++6.0 on Windows 2000 Pro, compiling in Debug mode... why would we get different outputs why does mine give an address when yours doesn't...
    SWEBS Web Server - http://swebs.sourceforge.net

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by stovellp
    Well, I'm trying to use the 'Foo' function to set the value of main()'s "Word" variable.

    This is what I got with a bit more experimentation:
    In your first example using Word[10], when you pass &Word, you are passing the address of the first element of the array -- you are not passing the address of the array, therefore the first example is no different than this:
    Code:
    int main()
    {
        char Word[10] = "Bye";
        Foo(&Word[0]);
    }
    In other words, all you did was equivalent to passing a char *.

    In the second example
    Code:
    char * Word = "Bye";
    you are passing the address of a pointer to char (a char **) when you state Foo(&Word). Therefore, your function will not work. A pointer to (char *) is not the same as a pointer to char.

    So in Foo, what you are asking for is not possible in C++, i.e. you want to change the value of what is pointed to without dereferencing the pointer. And the reasons for illegal operations is that Foo's parameter is a void pointer, which leaves your application wide open for these kinds of errors due to the lack of compile-time type checking.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Sep 2003
    Location
    Australia
    Posts
    30
    Thanks Paul, thats cleared it all up for me

    So without changing LPVOID Bar, is there any possible way for me to change the value of Word in C++? How would I dereference it anyway?

    This is what I understand of pointers:

    int * ptr = &someNumber;

    ptr stores an address, the address of someNumber.
    *ptr is the value stored at that address (is this dereferencing?)
    ptr is the address.
    What is &ptr?

    I'm really stuck on this one there must be a way to change Word.
    SWEBS Web Server - http://swebs.sourceforge.net

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by stovellp
    Thanks Paul, thats cleared it all up for me

    So without changing LPVOID Bar, is there any possible way for me to change the value of Word in C++? How would I dereference it anyway?
    Here is the problem:

    When you want to change the value of xxxxx in a function, you pass a pointer to xxxxxx. However, you want to change the value of the pointer to the char in Foo.

    In your case, xxxxxx is "pointer to char". Now you want to change the value of "pointer to char", which means you must pass a pointer to "pointer to char". Again, to change the value of something, you pass a pointer to that something, and the something you want to change is pointer to char.

    A simpler example is this:
    Code:
    void Foo( char *ptr )
    {
        ptr = new char[10];
    }
    
    int main()
    {
        char *p;
        Foo( p );  // does the value of p change?
    }
    The answer to the question "does the value of p change" is no. The goal of Foo was to change the pointer value within Foo, and to do that you must pass the address of the pointer, and Foo must be a function that dereferences the pointer.
    Code:
    void Foo( char **ptr )
    {
        *ptr = new char[10];
    }
    
    int main()
    {
        char *p;
        Foo( &p );  // does the value of p change?
    }
    Now the function does change the value of the pointer, because you passed a pointer to the pointer, and it was dereferenced and the value pointed to was assigned a new value in Foo().
    *ptr is the value stored at that address (is this dereferencing?)
    Yes.
    ptr is the address.
    What is &ptr?
    It is also a pointer, but it stores the address of the pointer ptr. Basically, a pointer is a type that is no different than an int, double, float, etc. It must be stored somewhere, and where it is stored is also a pointer.
    there must be a way to change Word.
    And I've illustrated how to change it. You can't do it any other way without changing the function, or writing a new function that handles char pointers correctly.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Sep 2003
    Location
    Australia
    Posts
    30
    Ok Paul, I've managed to peice together a working version of it here:

    PHP Code:
    #include <iostream>
    #include <windows.h>

    using namespace std;

    int Foo(LPVOID *Bar)
    {
        *
    Bar = new char[100];

        *
    Bar "Hello";
        
    cout << "Bar: " << (char *)*Bar << endl;
        return 
    0;
    }

    int main()
    {
        
    char Word "bye";
        
    Foo((LPVOID *)&Word);

        
    cout << "Word: " << Word << endl
        return 
    0;

    Now this works perfectly, except it means I have to change
    Foo (LPVOID Bar)
    to Foo(LPVOID * Bar) which I cannot do (because its a functions from a DLL... or can I?)

    Is it possible to make it LPVOID without the *? How could I do this?

    Also, since I have *Bar = new char[100], shouldn't I call delete on Bar after? How would I go about doing that? This is what I tried:
    delete [] *Bar;
    delete [] Bar; but neither of them worked.
    SWEBS Web Server - http://swebs.sourceforge.net

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

Featured