I'm not understanding the question. The ampersand obtains the "location" or pointer to that variable. You cannot take any type, outside of pointers, and randomly turn into a pointer, C++ doesn't allow this. You can only cast a pointer into another pointer.
I would suggest just passing the string by reference. If you're interacting with a C API, you can use c_str() and size(), create your own array type, or use whatever the C API lets you use.
Im tryingf to use C++, i know that the pointer is a memory address and the & sign i believe is a direct copy of the variable. am i right? What i want to know is what im doing is getting the memory address of the string or ariable and then the & symbol gets whats inside of it?
right and thats what im saying, so i made a pointer to str which is
*S = "String #1";
if i just outputted S it would give me a hexidecimal value of where its stored, but doing strfunction(&str, &str2); its assigning a copy of the string "String #1" to str therefore when i go to output it in main, the output is not the hex value but String #1, do you understand now? is that what is happening ingthe code?
No because the pointer doesn't point to anything as of yet. Initializing a pointer doesn't initialize or allocate memory. It only allocates the size of one pointer (generally 2/4/8 bytes) which is what you're asking for.
This only creates a pointer which contains virtual type data, not a string.
This creates a string. You can fetch its pointer by using the ampersand.
Also, in your above example, you're actually giving a pointer to a pointer. The type you're passing is actually std::string**, not std::string*.
NOTE: The only exception to this is C strings.
constchar* pString = "This is a test example";
This is rather anti-intuitive... but it allocates an array on the stack just large enough for the array + 1 (which its allowd to do since the string must be a constant literal) where the last element contains a null character. I will never suggest you do this no matter how much C I use. Null terminated strings are pretty laughable outside of when you have to iterate through every single character anyways.
int variable; //Basic variable
int *pVariable; //Uninitialized pointer
pVariable = &variable; //pVariable now points to variable
int test = 10;
int *pTest = &test;
variable = *pTest; //This "dereferences" pTest to fetch the data pTest points to
//variable is now equal to test
int & rVariable = variable; //reference to a variable but isn't a variable
int *&rpVariable = pVariable; //Reference to a pointer but isn't a pointer
Ok i watched some tutorials and i think i understand better, the guy explained it like i had no common sense which helped a ton
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int x = 70;
ptr = &x;
cout << *ptr << endl;
ok so int x = 70; and i made a pointer *ptr, then i assigned the memory address of x to ptr then de refernced it so i could see the contents of x? thats kind of like what you had pretty much, but the thing is i have to see it and it has to be explained in exactly the right way or i wont understand it. but am i right so far? how do i do that same thing with a function?
Myriad reasons, really. C++ isn't really a memory safe language. You're given a lot of responsibility in that respect. When the function is popped from the stack, so too are the local variables. Implementations aren't guaranteed to preserve that data. It could, for example, be overwritten with some arbitrary value, like zero. Or perhaps the memory location is no longer mapped into memory.
In any event, you're setting yourself up for a world of hurt. There's a reason the compiler spits out a warning.
also why dont i need the & operator in front of the function name?
The function returns a pointer to an integer, i.e. a memory location that points to an integer type. Locally, we've declared our local intPtr as a pointer to an integer. As these two match, there's no need for a reference operator.