Help with VS2012 Debugger, and char pointer arrays too?

Hey guys, thanks for reading.

I need to sort an array of char pointers, and I'm using quick sort to do so.

One part of quick sort is swapping elements, and so far every variation I try isn't letting me. I'll either get access violation errors if I initialize tmp, and when I don't, I get 'initialize tmp' errors. If you guys could tell me what I'm doing wrong, so I can learn from it, I would appreciate the insight.

I'm trying to watch the variables of my pointer array through the debugger on VS2012 to try and watch what the sorting is doing. When I'm first initializing it, it works (I'll add a comment in my code to show where), however immediately after, it only shows the value of the first index in the pointer array. If there's a way to view the value of every index in the pointer array, I would love that too.

Here's the code:

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
//PRE: Takes in array and an array index
//POST: pushes all indexes forward and puts the value at index 'index' at index n.
void pushArray(char* pointers[], int index, int n)
{
	char tmp = ' ';
	copyValues(&tmp, pointers[index]);

	for(int x = n; x<=index; x++)
	{
		copyValues(pointers[x+1], pointers[x]);
	}

	copyValues(pointers[n], &tmp);
}

//PRE: takes in pointer array and two elements to be swapped.
//POST: swaps two elements.
void swapArray(char* pointers[], int n, int m)
{
	char tmp = ' ';

	copyValues(&tmp, pointers[n]);
	copyValues(pointers[n], pointers[m]);
	copyValues(pointers[m], &tmp);

}

void quickSort(char* pointers[], int n, int m)
{
	int pivot = midpoint(n, m);
		
	if(m-n>0)
	{
		swapArray(pointers, pivot, n); //places pivot element where all values to left are less than the pivot.

		for(int i = n+1; i <= m; i++)
		{
			if(strCmp(pointers[pivot], pointers[i])>0)
				pushArray(pointers, i, n); //...all values below pivot get moved to index n after pushing the others forward.
		}

		quickSort(pointers, 0, pivot-1);
		quickSort(pointers, pivot+1, m);		
	}
}

int main ()
{
	char* pointers[SIZE];
	char yourword[SIZE];
	int length = 0;
	
	cout << "String to be sorted: ";

	cin >> yourword;

	length = getLength(yourword);

	for(int x = 0; x<length; x++) //THIS is where I can see every array element. After this loop ends, it all disappears except for the first element.
	{
		pointers[x] = &(yourword[x]);
	}

	quickSort(pointers, 0, length-1);

	displayPointersArray(pointers, length);

	return 0;
}
Last edited on
Topic archived. No new replies allowed.