Converting string to pointer

Pages: 12
Feb 5, 2013 at 2:14pm
I was wondering how do i convert this string to pointer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include "_Header.h"

using namespace std;

void passbyreference(int *x, int *z)
{
    *x = 66;
    *z = 900;
}

void strfunction(string *S)
{
    *S = "fdsfs";
}

int main()
{
    int sandy = 13;
    int int2 = 0;
    string *str;

    passbyreference(&sandy, &int2);
    strfunction(&str);

    cout << sandy << " " << int2 << " " << str << endl;

    return 0;
}
Last edited on Feb 5, 2013 at 2:14pm
Feb 5, 2013 at 2:28pm
You never actually make a string, change line 22 to string str, and things should work out.
Feb 5, 2013 at 10:26pm
ah i see, so how does the pointer know that str is a pointer if i dont put * in front of it? Is it because of the ampersand operator?
Feb 5, 2013 at 11:36pm
closed account (S6k9GNh0)
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.
Last edited on Feb 5, 2013 at 11:45pm
Feb 5, 2013 at 11:41pm
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?
Last edited on Feb 5, 2013 at 11:46pm
Feb 5, 2013 at 11:46pm
closed account (S6k9GNh0)
Again, I'm not sure I understand. The ampersand isn't a copy of the variable, it's a copy of the location of the variable (the pointer to the variable).
Feb 5, 2013 at 11:54pm
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?
Last edited on Feb 6, 2013 at 12:02am
Feb 6, 2013 at 12:50am
closed account (S6k9GNh0)
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.

 
std::string *pString;

This only creates a pointer which contains virtual type data, not a string.

 
std::string myString;

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.
 
const char* 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.
Last edited on Feb 6, 2013 at 12:54am
Feb 6, 2013 at 5:07am
Ok so now im back to being confused as hell again. But maybe i understand, so the * and the & are both pointers?
Feb 6, 2013 at 6:01am
closed account (S6k9GNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 
Last edited on Feb 6, 2013 at 6:02am
Feb 6, 2013 at 7:39am
ok the middle part is what i was trying to do. What exactly does "dereferencing" do?
Feb 6, 2013 at 9:12am
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int x = 70;
    int *ptr;

    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?

im confused here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include "Test.h"

using namespace std;

void function(int &A)
{
    int test = 9000;
    int *pTest;

    pTest = &test;
}

int main()
{
    int x = 70;
    int *ptr;

    ptr = &x;

    cout << *ptr << endl;
    cout << function() << endl;


}
Feb 6, 2013 at 9:48am
What exactly are you wanting the function to do?


Feb 6, 2013 at 10:25am
ok i want to get test from the function, and de reference it in main and output it to get 9000
Feb 6, 2013 at 10:32am
So you want to return the address of a local variable? It's doable, but dangerous in my opinion. I wouldn't ever think of doing it.

Nevertheless, here's how it can be done.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int* SomeFunc()
{
   int x = 9000;
   return &x;
}

int main( int argc, char* argv[] )
{
   int *intPtr;

   intPtr = SomeFunc();

   std::cout << "Pointing to " << *intPtr << " at " << intPtr << std::endl;
}
Feb 6, 2013 at 10:33am
Thanks, why is it dangerous? also why dont i need the & operator in front of the function name?

int *pointer2 = function();

like i do this

int *pointer1 = &x;

??
Last edited on Feb 6, 2013 at 10:51am
Feb 6, 2013 at 11:09am
Ch1156 wrote:
Thanks, why is it dangerous?

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.

Ch1156 wrote:
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.
Feb 6, 2013 at 11:13am
Ok thanks for the info, is there any way i can work with memeory safely? or learn how to use pointers safely so nothing bad happens?
Feb 6, 2013 at 11:31am
If you Google it, there'll be a ton of articles and posts on the safe use of pointers.

The main thing is just to be careful. Keep an eye on scope. If you use the heap, make sure you delete properly and use deep copying where necessary.
Feb 6, 2013 at 8:46pm
Ok i'll google all that stuff here in a minute. But i need to delete pointers? i though you only used the delete keyword when you use new?
Pages: 12