What is going on with my pointer?

ignore the return part of the function
I want to the user to input a string.
With that string I want to use a pointer to point to the string and pass it through a function.
When I print the contents of the pointer. It prints junk.

This is where I am stuck, passing a pointer to a string through a function.
Been stuck here for 5 hours googling how to pass a pointer to a string.

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
31
32
33
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

string encode(char *ptr);

int main()
{
	string original_message;
	
	cout << "Enter a message to be decoded: ";
	getline(cin, original_message);
	
	char *ptr = new char [original_message.length()+1];
	string encoded_message = encode(ptr);
	
	cout << encoded_message << endl;
	
	delete []ptr;
	return 0;
}
string encode(char *ptr)
{
	cout << ptr << endl;
	cout << *ptr << endl;
	cout << &ptr << endl;
	string c = "hi";
	string enco = c;
	
	return enco;
}
Last edited on
UPDATE:
Is this correct?
http://www.cplusplus.com/forum/beginner/140740/
I read keskiverto's response.

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
31
32
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

string encode(string *ptr);

int main()
{
	string original_message;
	
	cout << "Enter a message to be decoded: ";
	getline(cin, original_message);
	
	string *ptr = &original_message;
	string encoded_message = encode(ptr);
	
	cout << encoded_message << endl;
	
	return 0;
}
string encode(string *ptr)
{
	cout << ptr << endl;
	cout << *ptr << endl;
	cout << &ptr << endl;
	string c = "hi";
	string enco = c;
	
	return enco;
}
Last edited on
closed account (SECMoG1T)
yea the second approach is correct, but your encoding function doesn't seem to be doing anything important.

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 <cstring>

using namespace std;

string encode(string *ptr);

int main()
{
	string original_message;

	cout << "Enter a message to be decoded: ";
	getline(cin, original_message);

	string *ptr = &original_message;
	string encoded_message = encode(ptr);///correct

	cout << encoded_message << endl;

	return 0;
}

string encode(string *ptr)
{
   cout << *ptr << endl;
   ///code for encoding your string
   return *ptr;///assuming the encode have completed
}


you can also use smart pointers, they are much safer than raw pointers.
closed account (SECMoG1T)
i have noticed you are having problems passing pointers to functions, this is how it is done

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <memory> ///smart pointers

void print(std::string* str_ptr)///the argument must match type std::string*
{
    std::cout<<*str_ptr<<"\n\n";
}

void print_smart_pointer(std::shared_ptr<std::string> ptr)
{
    std::cout<<*ptr<<"\n\n";
}

///this is a different function, it accepts a c string not an std::string
void print_cstring(const char* c_str)
{
    std::cout<<c_str<<"\n";
}

int main()
{
    std::string hello("hello there\n");
    std::string* hello_ptr = &hello;

    print(hello_ptr);///how to pass the argument

    std::string* str_ptr = new std::string("pointer to dynamically allocated string\n");

    print(str_ptr);///yhow to pass the argument

    std::shared_ptr<std::string> smart_str_ptr = std::make_shared<std::string>("a string pointed to by a smart pointer\n\n");

    print_smart_pointer(smart_str_ptr);///how to pass the argument

    ///c strings

    const char* cstr_ptr ="hello therem\0";

    print_cstring(cstr_ptr);///how to pass the argument

    char cstr[12] ={'h','e','l','l','o',' ','t','h','e','r','e','\0'};

    print_cstring(cstr);///how to pass the argument

}
@Yolanda your code scares me. I am a beginner.
closed account (SECMoG1T)
ooh sorry, i meant it for you to look at how you create a function that takes a pointer see
number 5 and 16.

from line 21, i was showing you how to create pointer variables and then pass them to the function we created on line 5 and 16.
Hardest part of the program
passing a point to a string

Rules
Encode function
Consonsants and vowels (capital or lowercase) moves 9 to the right
A->J
numbers are turned into capital letters
0 -> A................9->J
Decode function goes back to user input
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//finished code
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <vector>

using namespace std;

string encode(string *eptr, vector<int> &v);
string decode(string *dptr, const vector<int> &v);

int main()
{
	string original_message;
	vector<int> position;
	
	cout << "Enter a message to be decoded: ";
	getline(cin, original_message);
	
	string *ptr = &original_message;
	string encoded_message = encode(ptr, position);
	cout << "encoding: " << encoded_message << endl;
	ptr = &encoded_message;
	string decoded_message = decode(ptr, position);
	cout << "decoding: " << decoded_message << endl;
	
	return 0;
}
string encode(string *eptr, vector<int> &v) //this function can be shorter
{
	string storemessage = *(eptr);
	string newmessage = "\0";
	int convertASCII = 0;
	for(int i = 0;i < eptr->length();i++)
	{
		if ((int)storemessage[i] > 64 && (int)storemessage[i] < 82) //A-Q
		{
			convertASCII = (int)storemessage[i];
			convertASCII += 9;
			newmessage += static_cast<char>(convertASCII); //int to ascii
		}
		if((int)storemessage[i] > 81 && (int)storemessage[i] < 91) //R-Z
		{
			convertASCII = (int)storemessage[i];
			convertASCII -= 17;
			newmessage += static_cast<char>(convertASCII);
		}
		if((int)storemessage[i] > 96 && (int)storemessage[i] < 114) //a to q
		{
			convertASCII = (int)storemessage[i];
			convertASCII += 9;
			newmessage += static_cast<char>(convertASCII); //int to ascii
		}
		if((int)storemessage[i] > 113 && (int)storemessage[i] < 123)//r-z
		{
			convertASCII = (int)storemessage[i];
			convertASCII -= 17;
			newmessage += static_cast<char>(convertASCII);
		}
		if((int)storemessage[i] > 47 && (int)storemessage[i] < 58) //numbers
		{
			v.push_back(i);
			convertASCII = (int)storemessage[i];
			convertASCII += 17;
			newmessage += static_cast<char>(convertASCII);
		}
	}
	
	return newmessage;
}

string decode(string *dptr, const vector<int> &v)
{
	string storemessage = *(dptr);
	string newmessage = "\0";
	int convertASCII = 0;
	int j = 0;
	for(int i = 0;i < dptr->length();i++)
	{
		if(i == v[j]) //v is a vector that holds the positions of the numbers
		{
			convertASCII = (int)storemessage[i];
			convertASCII -= 17;
			newmessage += static_cast<char>(convertASCII);
			j++;
		}
		else if ((int)storemessage[i] > 64 && (int)storemessage[i] < 91) //A-Z
		{
			convertASCII = (int)storemessage[i];
			convertASCII -= 9;
			if(convertASCII < 65)
			{
				convertASCII = 90 - (65 - convertASCII) + 1;
			}
			newmessage += static_cast<char>(convertASCII);
		}		
		else if((int)storemessage[i] > 96 && (int)storemessage[i] < 123) //a to z
		{
			convertASCII = (int)storemessage[i];
			convertASCII -= 9;
			if(convertASCII < 97)
			{
				convertASCII = 122 - (97 - convertASCII) + 1;
			}
			newmessage += static_cast<char>(convertASCII);
		}
	}
	
	return newmessage;
}
Last edited on
Hardest part of the program
passing a point to a string
What is the problem? You pass a pointer. That's it. You can shorten lines 21 to 25:
1
2
3
4
	string encoded_message = encode(&original_message, position);
	cout << "encoding: " << encoded_message << endl;
	string decoded_message = decode(&encoded_message, position);
	cout << "decoding: " << decoded_message << endl;
I.e. you don't need the variable ptr.
Topic archived. No new replies allowed.