Issues passing a pointer pointing to a struct to a function

I am running into an issue that I cannot seem to resolve.

My prototype functions are,
1
2
void display(const Kingdom& pKingdom);
void display(const Kingdom& pKingdom, int count);


And my implementations are,
1
2
3
4
5
6
7
8
9
10
void display(const Kingdom& pKingdom) {
	cout << pKingdom.m_name << ", " << "population " << pKingdom.m_population << endl;
}

void display(const Kingdom& pKingdom, int count) {
	cout << "Kingdoms are" << endl;
	for (int i = 0; i < count; i++) {
		cout << i + 1 << ". " << pKingdom.m_name << ", population " << pKingdom.m_population << endl;
	}
}


When I call the functions,

 
display(pKingdom[0]);


works without issues, but,

 
display(pKingdom[count], count);


doesn't seem to like what is being passed to it/the way it is being passed to it.

I get some garbage results where it says
 
Kingdoms are


Image: https://i.imgur.com/E7JoFSG.png

Any thoughts? The structure itself is,
1
2
3
4
struct Kingdom {
	char m_name[32];
	int m_population;
};
Last edited on
Your function prototype doesn't match what you're trying to pass into it. Perhaps your second prototype should be:

1
2
3
4
5
6
7
8
9
10
void display(const Kingdom& pKingdom);
void display(const Kingdom kingdoms[], size_t count);

void display(const Kingdom kingdoms[], size_t count) {
	cout << "Kingdoms are" << endl;
	for (size_t i = 0; i < count; i++) {
               display(kingdoms[i]);
	}
}


Any thoughts? The structure itself is,

Why the C-string?
1
2
3
4
struct Kingdom {
	std::string m_name;
	int m_population;
};

Using a std::string would be safer in most instances. Also if you used a std::vector instead of the array you could pass by reference and not need the count parameter.


Last edited on
@jlb

Hello and thank you for your response; we havent learned the vector and string objects yet, unfortunately.

I implemented your response, and I am getting another error:
https://i.imgur.com/J2Cxno1.png

It's telling me no operator matches those operands.. which is weird because Im simply indicating that I want the first cell of pKingdom, no?

Thank you again,
You need to post your code along with the complete error message, all of them, exactly as they appear in your development environment.

@jlb

Error message:
1
2
no operator "[]" matches these operands
operand types are: const sict::Kingdom[ int ]


//header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef KINGDOM_H
#define KINGDOM_H
namespace sict
{
		struct Kingdom {
			char m_name[32];
			int m_population;
		};
		void display(const Kingdom& pKingdom);
		void display(const Kingdom kingdoms[], size_t count);
}


#endif 


//implementation file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "Kingdom.h"
using namespace std;
namespace sict
{
	void display(const Kingdom& pKingdom) {
		cout << pKingdom[0].m_name << ", " << "population " << pKingdom[0].m_population << endl;
	}





	void display(const Kingdom kingdoms[], size_t count) {
		cout << "Kingdoms are" << endl;
		for (size_t i = 0; i < count; i++) {
			display(kingdoms[i]);
			//cout << i + 1 << ". " << kingdoms[i].m_name << ", population " << kingdoms[i].m_population << endl;
		}
	}
}


//main
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

#include <iostream>
#include <cstring>
#include "Kingdom.h"

using namespace std;
using namespace sict;

void read(Kingdom&);

int main() {
	int count = 0; // the number of kingdoms in the array
	Kingdom *pKingdom = nullptr;
	cout << "==========\n"
		<< "Input data\n"
		<< "==========\n"
		<< "Enter the number of Kingdoms: ";
	cin >> count;
	cin.ignore();

	if (count < 1) return 1;

	pKingdom = new Kingdom[count];

	for (int i = 0; i < count; ++i) {
		cout << "Kingdom #" << i + 1 << ": " << endl;
		// TODO: add code to accept user input for Kingdom i
		read(pKingdom[i]);
	}
	cout << "==========" << endl << endl;

	// testing that "display(...)" works
	cout << "------------------------------" << endl
		<< "The 1st kingdom entered is" << endl
		<< "------------------------------" << endl;
	display(pKingdom);
	cout << "------------------------------" << endl << endl;

	// expand the array of Kingdoms by 1 element
	count = count + 1;
	Kingdom *cpy_pKingdom = nullptr;
	cpy_pKingdom = new Kingdom[count + 1];
		cpy_pKingdom = pKingdom;
		delete[] pKingdom;
		pKingdom = cpy_pKingdom;
	// add the new Kingdom
		cout << "==========\n"
			<< "Input data\n"
			<< "==========\n";
		
			cout << "Kingdom #" << count << ": " << endl;
			read(pKingdom[count + 1]);
					
	cout << "==========\n" << endl;

	display(pKingdom, count);
	cout << endl;
	delete[] pKingdom;
	getchar();
	return 0;
}
void read(Kingdom& pkingdom) {
	cout << "Enter the name of the Kingdom: ";
	cin.get(pkingdom.m_name, 32, '\n');
	cin.ignore(2000, '\n');
	cout << "Enter the number of people living in " << pkingdom.m_name << ": ";
	cin >> pkingdom.m_population;
	cin.ignore(2000, '\n');
}
Why did you change the implementation of the first display() function? It should have remained the same.

1
2
3
void display(const Kingdom& pKingdom) {
	cout << pKingdom.m_name << ", " << "population " << pKingdom.m_population << endl;
}


By the way those error messages are not the complete compiler error messages. The compiler error messages will give much more information, such as the line number where the error was detected.

Also your function call on line 36 is also incorrect. If you want to print a single Kingdom you need to pass a single Kingdom not the array.



Last edited on
@jlb

The reason I changed it was because when I had it this way, as you just replied, it was throwing garbage out at me. Now having it as you put it, the Call Stack says
 
 	[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]		Annotated Frame


throwing some sort of exception. Here's an image of the output: https://i.imgur.com/qaXwJ6F.png

It seems to be passing an array that points(?) to garbage..
Sorry for the bother, but Im just learning C++ and Ive been at this particular issue for the past 3 hours
Last edited on
The exception is probably happening because you're trying to delete your memory twice.

Also the "garbage" display is not being caused by the display functions, but is probably being caused by "bad data". I really don't know what you're trying to do in the following snippet, but I suspect that whatever you're doing is not correct.

1
2
3
4
5
6
7
8
9
10
11
12
	Kingdom *cpy_pKingdom = nullptr;
	cpy_pKingdom = new Kingdom[count + 1];
		cpy_pKingdom = pKingdom;
		delete[] pKingdom;
		pKingdom = cpy_pKingdom;
	// add the new Kingdom
		cout << "==========\n"
			<< "Input data\n"
			<< "==========\n";

			cout << "Kingdom #" << count << ": " << endl;
			read(pKingdom[count + 1]);


With the following program:
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
#ifndef KINGDOM_H
#define KINGDOM_H
#include <cstdlib>

namespace sict
{
		struct Kingdom {
			char m_name[32];
			int m_population;
		};
		void display(const Kingdom& pKingdom);
		void display(const Kingdom kingdoms[], size_t count);
}


#endif
#include <iostream>
//#include "Kingdom.h"
using namespace std;
namespace sict
{
    void display(const Kingdom& pKingdom) {
        cout << pKingdom.m_name << ", " << "population " << pKingdom.m_population << endl;
    }

	void display(const Kingdom kingdoms[], size_t count) {
		cout << "Kingdoms are" << endl;
		for (size_t i = 0; i < count; i++) {
			display(kingdoms[i]);
			//cout << i + 1 << ". " << kingdoms[i].m_name << ", population " << kingdoms[i].m_population << endl;
		}
	}
}
#include <iostream>
#include <cstring>
//#include "Kingdom.h"

using namespace std;
using namespace sict;

void read(Kingdom&);

int main() {
	int count = 0; // the number of kingdoms in the array
	Kingdom *pKingdom = nullptr;
	cout << "==========\n"
		<< "Input data\n"
		<< "==========\n"
		<< "Enter the number of Kingdoms: ";
	cin >> count;
	cin.ignore();

	if (count < 1) return 1;

	pKingdom = new Kingdom[count];

	for (int i = 0; i < count; ++i) {
		cout << "Kingdom #" << i + 1 << ": " << endl;
		// TODO: add code to accept user input for Kingdom i
		read(pKingdom[i]);
	}
	cout << "==========" << endl << endl;

	// testing that "display(...)" works
	cout << "------------------------------" << endl
		<< "The 1st kingdom entered is" << endl
		<< "------------------------------" << endl;
	display(pKingdom[0]);
	cout << "------------------------------" << endl << endl;

	// expand the array of Kingdoms by 1 element
	count = count + 1;
	Kingdom *cpy_pKingdom = nullptr;
	cpy_pKingdom = new Kingdom[count + 1];
		cpy_pKingdom = pKingdom;
		delete[] pKingdom;
		pKingdom = cpy_pKingdom;
	// add the new Kingdom
		cout << "==========\n"
			<< "Input data\n"
			<< "==========\n";

			cout << "Kingdom #" << count << ": " << endl;
			read(pKingdom[count + 1]);

	cout << "==========\n" << endl;

	display(pKingdom, count);
	cout << endl;
//	delete[] pKingdom;
	getchar();
	return 0;
}
void read(Kingdom& pkingdom) {
	cout << "Enter the name of the Kingdom: ";
	cin.get(pkingdom.m_name, 32, '\n');
	cin.ignore(2000, '\n');
	cout << "Enter the number of people living in " << pkingdom.m_name << ": ";
	cin >> pkingdom.m_population;
	cin.ignore(2000, '\n');
}


I get this output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
==========
Input data
==========
Enter the number of Kingdoms: 1
Kingdom #1: 
Enter the name of the Kingdom: test
Enter the number of people living in test: 4
==========
The 1st kingdom entered is
------------------------------
test, population 4
------------------------------

==========
Input data
==========
Kingdom #2: 
Enter the name of the Kingdom: George
Enter the number of people living in George: 34
==========

Kingdoms are
, population 4
, population 0


@jlb

I am deallocating memory to avoid memory leaks, hence the delete[]

How can I go about fixing this? I've heard std::vector is a way to go, but we havent learned that yet, and we're supposed to work with what I already have coded in.

This is so confusing, I do not understand what is going on; it is compiling and the syntax seems to be ok
I am deallocating memory to avoid memory leaks, hence the delete[]

Yes but you need to insure you only delete[] the same memory once.

How can I go about fixing this? I've heard std::vector is a way to go,

This is a shame since std::vector is the best way to fix this issue.

This is so confusing, I do not understand what is going on; it is compiling and the syntax seems to be ok

Yes dealing with dynamic memory can get very confusing, which is why std::vector is so highly recommended. However since you must deal with arrays you need to copy each element of the array to the new position, don't try to use assignment.

One thing you may want to do is to keep the capacity of the array separate from your current counter. Just use capacity to size/resize the arrays let your counter keep track of which element you are currently working with.

Here is the resizing of the array, at this point capacity is the value you entered for the number of Kingdoms, count is the number of initialized elements elements currently in the array. The count should always be less than or equal to the capacity. The capacity must be be greater than zero and less than or equal to size_t::max.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    // Increase the size of the array by 1.
    Kingdom *cpy_kingdom;
    capacity++;
    cpy_kingdom = new Kingdom[capacity];

    // Copy the original array to the new array.
    for(size_t i = 0; i < count; ++i)
        cpy_kingdom[i] = pKingdom[i];

    // Delete the memory for the original array.
    delete[] pKingdom;

    // Now copy the pointer from the new array to the original array.
    pKingdom = cpy_kingdom;  // Both pointers now pointing to the same place.



Here is the code that follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    // add the new Kingdom
    cout << "==========\n"
         << "Input data\n"
         << "==========\n";

    cout << "Kingdom #" << count << ": " << endl;
    read(pKingdom[count++]);   // Note the increment of count is here, but after the read().

    cout << "==========\n" << endl;

    display(pKingdom, count);
    cout << endl;

    delete[] pKingdom;


Topic archived. No new replies allowed.