If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

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?

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.

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.

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.

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.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.