Arrays as Structure Members


#include <iostream.h>

struct ArrayStruct{
	int IntArray[5];
	int size;
};

void changeArray(ArrayStruct );

int main(){
	ArrayStruct MyStruct;
	for (int i=0; i<MyStruct.size; i++)
		MyStruct.IntArray[i] = i;
	MyStruct.size = 5;

	cout << "Before function call, the array is..." << endl;
	for (int i=0; i<MyStruct.size; i++)
		cout << MyStruct.IntArray[i] << ' ';
	cout << endl;

	changeArray(MyStruct);

	cout << "\nAfter function call, the array is..." << endl;
	for (int i=0; i<MyStruct.size; i++)
		cout << MyStruct.IntArray[i] << ' ';
	cout << endl;

	return(0);
} 

void changeArray(ArrayStruct Astruct){
	for (int i=0; i<Astruct.size; i++)
		Astruct.IntArray[i] += 10;
	cout << "\nInside the function the array is..." <<
endl;
	for (int i=0; i<Astruct.size; i++)
		cout << Astruct.IntArray[i] << ' ';
	cout << endl;
}

Output:

Before function call, the array is...
0 1 2 3 4 

Inside the function the array is...
10 11 12 13 14 

After function call, the array is...
0 1 2 3 4 



Back to Previous Page

Document:
Local Date:
Last Modified On: