Problem accessing array of struct type

Hello, I've been trying to access the elements of an array of a struct type, and looking at other articles on this site I thought my syntax was correct. On line 31 I want to print out values from the array, but when I run the program I get the following error: "STATIC_ACCESS_VIOLATION". I'm just trying to figure out how to access member variables within array elements. Any suggestions?

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

using namespace std;

struct person
{
    string firstName, lastName;
    int age;
};

person input();
void makeFriends(person arrayParameter[]);


int main()
{
	person john; 
	person * person_ptr; 
	person * friends_ptr = new person[5]; 

	john = input();

	person_ptr = &john;

	cout << "\nThe memory location of the struct object is " << person_ptr << ".\n"
		 << "\nThe the value of the object's firstName member variable is " << person_ptr->firstName << ".\n";
	
	makeFriends(&friends_ptr[5]);

	cout << endl << friends_ptr[0].firstName << endl;

	system("pause");

	return 0;
}

person input()
{
    person newPerson;

	newPerson.firstName = "John";
	newPerson.lastName = "Doe";
	newPerson.age = 42;

    return newPerson;
}

void makeFriends(person arrayParameter[])
{
	person friend1 = arrayParameter[0];
	friend1.firstName = "Bill";
	friend1.lastName = "Johnson";
	friend1.age = 23;

	person friend2 = arrayParameter[1];
	friend1.firstName = "Sylvie";
	friend1.lastName = "Liebermann";
	friend1.age = 32;

	person friend3 = arrayParameter[2];
	friend1.firstName = "Ronald";
	friend1.lastName = "McDonald";
	friend1.age = 62;

	person friend4 = arrayParameter[3];
	friend1.firstName = "Toki";
	friend1.lastName = "Wartooth";
	friend1.age = 28;

	person friend5 = arrayParameter[4];
	friend1.firstName = "Elsa";
	friend1.lastName = "Josenhans";
	friend1.age = 19;
}
Please explain lines 29 and 51.
Last edited on
My understanding is that you cannot return an array from a function in C++, and so you have to use a pointer in combination with a void function to assign values there. Correct me if this is wrong. On line 29 I am calling the makeFriends function, passing in the pointer to the array (or at least that was the intent). On line 51 I am declaring a new object of type person, and assigning it the the first element of an array. I copied the syntax from here: http://www.cplusplus.com/forum/beginner/56820/
However, in that thread the array is passed differently and there is no copy assignment to local object.

Precedence. &foo[4] does first dereference foo[4] and then returns the address of that element.
In array of 5 there is no array[5].

When you make a copy from an array element and then change that copy, the array does not change.
Also, as a side note, functions can return arrays, since arrays are just pointers after all. Just don't declare the array as an array, rather as a new allocated pointer. Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int* makeArray() {
    int* arr = new int[6];
    // allocate values
    arr[0] = 1;
    arr[1] = 1;
    arr[2] = 2;
    arr[3] = 3;
    arr[4] = 5;
    arr[5] = 8;
    // return the array
    return arr;
}

int main() {
    int* arr;
    arr = makeArray();

    // do something

    // still need to delete the array!
    delete[] arr;
    return 0;
}


As for how to pass an array to a function taking an array as a parameter (technically a pointer), just pass the name of the array:
makeFriends(friends_ptr);

Just note that doing it this way means you are passing the pointer by value, so even though the memory the pointer is pointing to will change the pointer itself can't, so you can't reallocate or 'null' the pointer this way. If you want to do this with a function, you will need to pass the pointer by reference:
void doSomething(int*& arr); // reference to a pointer

Hope this clears up some things for you.
Thanks guys, both your comments help tremendously and I should be able to fix my code!
Topic archived. No new replies allowed.