Pointer Mayhem

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
	Encode and decode a string using bitwise rotation
	by a value (number of places) derived from a string key.
*/

#include <iostream>
using namespace std;


unsigned char lrotate(unsigned char val, int n);
unsigned char rrotate(unsigned char val, int n);
void show_binary(unsigned char u);


int main()
{
	char *str = "This is yet another test.";	// strings
	char *key = "Shazam";

	int slen = (int) strlen(str);	// string lengths
	int klen = (int) strlen(key);
	int rotnum;		// number of places to rotate

	cout << "Original string: " << str << "\n";
	
	// encode message using funny 
	// number of places algorithm based on length of key
	//
	for(int i = 0; (*str); i++)
	{
		rotnum = (int) ((key[i % klen] ^ i) % slen); // limit to string length
		
		*str = rrotate((*str), rotnum);	// *** PROBLEM ***
		str++;

	}
		
	cout << "\nEncoded string: " << str << "\n";

	// decode message by rotating in opposite direction
	//
	for(int i = 0; (*str); i++)
	{
		rotnum = (int) ((key[i % klen] ^ i) % slen);
		
		*str = lrotate((*str), rotnum);	// *** PROBLEM ***
		str++;

	}

	cout << "Decoded string: " << str << "\n";

	return 0;
}


// left rotate a byte n places
//
unsigned char lrotate(unsigned char val, int n)
{
	unsigned int t;
	t = val;

	for(int i = 0; i < n; i++)
	{
		t <<= 1;

		/*
			if a bit shifts out it'll be in bit 8
			of the integer t. If this is the case
			put the bit on the right side
		*/

		if(t & 256) t |= 1;	// switch on bit 0

	}

	return t;

}	// end lrotate()


// right rotate a byte n places
//
unsigned char rrotate(unsigned char val, int n)
{
	unsigned int t;	// integers have 32 bits
	t = val;

	// first move val to the next higher 8 bits
	t <<= 8;

	for(int i = 0; i < n; i++)
	{
		t >>= 1;

		/*
			if a bit shifts out it'll be in bit 7
			of the integer t. If this is the case
			put the bit on the left side
		*/

		if(t & 128) t |= 32768;	// switch on bit 15 
					// 1000 0000 0000 0000

	}

	// put the result back 
	// in the lower bits of t
	t >>= 8;

	return t;

}	// end rrotate()


// display the bits within a byte
//
void show_binary(unsigned char u)
{
	int t;
	for(t = 128; t > 0; t /=2)
	{
		if(u & t) cout << "1";
		else cout << "0";
	}

	cout << "\n";
}

Hi again.
I'm a bit of a newbie with pointers and can't figure why
I keep running into the PROBLEM highlighted above ***

I'm simply trying to rotate bits as some sort of rudimentary encryption.
This works without pointers. And the above code compiles without warnings.

So this pointer version keeps running into the problem as follows.

As I loop through the string using pointer arithmetic I can pass the character that's current in the loop to the rotation functions OK by dereferencing the pointer. No problems there.

As soon as I try to set the value it points at to the rotated result it bombs out. This doesn't make much sense to me, so just wanted to ask if anyone may have a solution or be so kind as to explain why the pointer fun is turning into mayhem.
Last edited on
Sry I think I get it. Using strings the way I have above
keeping only the address causes them to be stored as const char arrays?

So I'm trying to update the constant and there...

So we can only store C++ strings like above if
they're to be unchanged in the program.

Hence I'll have to use the char[] form and
store the string in the program so I can modify it?

Perhaps I just answered to my own problem. Thanks anyway.
Last edited on
You are absolutely correct. And that is one of my pet peeves about C++.

char* foo = "This is a string";

is really an invalid cast, because the string in reality is a const char*, because string literals typically get compiled into non-writable pages (ie, code pages).
Topic archived. No new replies allowed.