Passing pointers question

Hello all,

I have been working on an assignment using dynamic arrays and was wondering why I was able to alter my array sent from main() using the following function parameters:

void newRemove(call_record *call_DB, int & count, const string key);

when i was under the impression that I would need the *& operators between call_record and call_DB to modify it within the function being invoked. For example, when we give the array its initial values, we used:

void initialize(call_record *& call_DB, int & count, int & size);

Here is all of the relevant 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
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

void newRemove(call_record *call_DB, int & count, const string key);

int main()
{
	int size = 5; //total amount of memory (cells) allocated for the dynamic array of call records
	int count = 0;
	call_record *call_DB;
	call_DB = new call_record[size];
	string userCellNumber = "0";
	char userOption = 'z';
	bool userOptConf = false;

	initialize(call_DB, count, size);
	cout << "Input data from .txt file has been placed into the call database..." << endl << endl;
	
	do
	{
		
		userOption = 'z';
		cout << "Please carefully choose one of the following options..." << endl;
		cout << "Print database: \t\tP" << endl;
		cout << "Add new caller information: \tA" << endl;
		cout << "Remove caller information: \tR" << endl;
		cout << "Quit program: \t\t Q" << endl;
		cout << "Selection:  ";
		
		do {
			cin >> userOption;
			if (!cin)
			{
				cin.clear();
				cin.ignore(numeric_limits<streamsize>::max(), '\n');
				cout << "Error occurred... Please retry input..." << endl;

			}
			else
			{
				userOptConf = true;
			}
		} while (userOptConf != true);


		if (userOption == 'A' || userOption == 'a')
		{
			cout << endl << "Please enter cell phone number of new record:  ";
			cin >> userCellNumber;
			add(call_DB, count, size, userCellNumber);
			cout << endl;
		}
		else if (userOption == 'P' || userOption == 'p')
		{
			cout << endl;
			print(call_DB, count);
		}
		else if (userOption == 'R' || userOption == 'r')
		{
			cout << endl << "Please enter phone number to be deleted out of database:  ";
			cin >> userCellNumber;
			newRemove(call_DB, count, userCellNumber);
			cout << endl;
		}
		else if (userOption == 'Q' || userOption == 'q')
		{
			cout << endl << "Preparing to exit program..." << endl;
		}
		else
		{
			cout << endl << "Error occurred... Please retry input..." << endl << endl;
		}
	} while (userOption != 'Q' && userOption != 'q');


	destroy_call_DB(call_DB);

	cout << endl << "Thank you..." << endl << endl;
	
	return 0;
}

void newRemove(call_record *call_DB, int & count, const string key)
{

	int i = 0, j = 0, k = 0, cellsRemoved = 0;

	for (i = 0; i < count; i++)
	{
		j = search(call_DB, count, key);
		if (j != -1)
		{
			cellsRemoved++;
			call_DB[j].firstname = "null";
		}
	}

	if (cellsRemoved == 0)
	{
		cout << endl << "Cell phone number not found..." << endl << endl;
	}

	count -= cellsRemoved;

	return;
}
Last edited on
I too am having the exact same issue!!! Could anyone please explain???
void initialize(call_record *& call_DB, int & count, int & size);
This is passing a reference to call_DB. call_DB is a pointer. That means initialize() can change the value of the pointer itself. Presumably it allocates space or reads the database or something.
 
void newRemove(call_record *call_DB, int & count, const string key);

This just passes the the pointer by value. If newRemove() changes the value of the parameter, it won't change the value of the argument that was passed into into it.

So how can newRemove actually remove anything from the database? The key is that you're using dynamic arrays. newRemove doesn't change call_DB, it changes the items that it points to. Specifically, it looks like it removes one item from the array and changes count to reflect the number of items now in the array.
Topic archived. No new replies allowed.