Structured array (Pointers?)

If I have a structure for an array, like this,
1
2
3
4
5
6
struct ARRAY_STRUCT {
int num1;
int num2;
char char1;
char char2;
}

and I want to use variables in the array, like this,
1
2
3
4
5
6
7
8
int x;
int y;
char a;
char b;
ARRAY_STRUCT array1 = {
     { 5, 5, 'm', 'n' },
     { x, y, a, b },
}

how could I edit the variables after it is initialized? To my understanding I need to use pointers, is there any article for pointers and structured arrays specifically? Any tips or alternate solutions would be much appreciated.
I apologize in advance for the sloppy code example above.
Hi Jim,

You don't need pointers for that. Pointers are variables that store the address to another variable (they "point" to where it is in memory). In your case, you almost got it right. Here is what you should do:

Create a header file where you declare your structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef __FORUM_CPPREF_85646__
#define __FORUM_CPPREF_85646__

#include <iostream>
using namespace std;

struct DataContainer
{
	int x, y;
	char a, b;	
};

#endif 


Create an implementation that uses this structure:
1
2
3
4
5
6
7
8
9
10
11
#include "example_85646.h"

int main(int argc, char const *argv[])
{
	DataContainer c[2] = 
		{
			{ 1, 2, 'a', 'b' },
			{ 42, 7, 'm', 'a' }
		};
	cout << c[1].a << endl;
}


Notice that variable c is an array, and therefore, you should use the square brackets ([]) to access its elements. Careful, indices begin at 0, not 1. Once you have an element of "c", you know it's a DataContainer object, and you can dereference its member "a" using the dot ".".

EDIT: Next time, I think you should post this kind of question in the beginners forum. But always glad to help!
Last edited on
No, I understand how arrays work. I am not exactly a beginner. I have been working with code for over a year now. I know how to access sections of a structured array.

My question was how to edit variables in an array after it is initialized. So, using your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
	int num1 = 5;
	int num2 = 7;
	char char1 = 'a';
	char char2 = 'b';
	DataContainer c[2] = 
		{
			{ num1, num2, char1, char2 },
			{ 42, 7, 'm', 'a' }
		};
	cout << c[1].a << endl;
	num1 = 9;
	cout << c[1].a << endl;
}
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
int main()
{
	struct ARRAY_STRUCT {
		int num1;
		int num2;
		char char1;
		char char2;
	};


	ARRAY_STRUCT array1 = {
	     { 5, 5, 'm', 'n' },
	     { 6, 6, 'o', 'p' },
	};

	array1[0].num1 = 50;
	array1[0].num2 = 50;
	array1[0].char1 = 'M';
	array1[0].char2 = 'N';

	array1[1].num1 = 60;
	array1[1].num2 = 60;
	array1[1].char1 = 'O';
	array1[1].char2 = 'p';
}
I strongly recommend that you don't do that.
Use a syntax like c[i].x to change the field x of element i, instead of keeping a variable that points to this element, it's safer, and much clearer that way for anybody who will read your code.

What if you create your array somewhere, then pass the variable to somebody else, then the scope in which you created your array is destroyed.. How will you make sure that your variable still points to something when you will use it? Appart from tracing back the call sequence manually in your program from the moment it has been initialized, you can't; that's very dangerous, and it might get unmaintainable very fast if your program gets big. In short it's a bad habit to take.

That being said, you were right then, you need a pointer:
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
#include "example_85646.h"

int main(int argc, char const *argv[])
{
	// The value to assign is in a variable
	char a = 'a';

	// Create our pointer
	char *j = &a;

	// Here is your container
	DataContainer c[2] = 
		{
			{ 1, 2, *j, 'b' },
			{ 42, 7, 'm', 'a' }
		};

	// Show the assignment
	cout << c[0].a << endl;

	// Reassign the address stored in j
	j = &( c[0].a );

	// Modify the value pointed by j
	*j = 'j';

	// Show the change
	cout << c[0].a << endl;
}
Last edited on
Hi again Jim,

If my last post answered your question, could you please mark the thread as solved? Thanks
closed account (zb0S216C)
You don't need pointers to access an array. Simply applying the sub-script operator ([...]) to the array with an index will obtain the object at the corresponding index. For example:

1
2
3
int Array_[3];

Array_[0];

The second statement in the above code attempts to access the first "int" within the array. Note that an "index" is value that identifies an element of an array. Also note that indices begin at 0 (the first element) and end at n-1 (n is the array's length). Internally, accessing an array in this form is equivalent to this:-

 
*( Array_ + Index );

Note the asterisk -- the asterisk means we're dereferencing the resulting address of the "Array_ + Index" expression. You should also note that arrays are not pointers.

Wazzak
Thank you for the advice Terminux, I wasn't aware that it was such bad habit to do it like this. I just felt as though with my actual code (I'm in the beginning stages of game development) it would be tedious and occasionally impossible to access and change the array manually instead of just changing a pointer to a variable.

Thank you all for your help!
Topic archived. No new replies allowed.