0% found this document useful (0 votes)
3 views

Tutorial (Array and Structure)

This tutorial covers how to pass and return arrays and structures in C++. It demonstrates passing arrays by value or pointer, and structures by value or reference, along with examples of printing and incrementing array values. Additionally, it explains how to return arrays and structures from functions, highlighting the differences between passing by value and reference.

Uploaded by

Ahmad Raza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Tutorial (Array and Structure)

This tutorial covers how to pass and return arrays and structures in C++. It demonstrates passing arrays by value or pointer, and structures by value or reference, along with examples of printing and incrementing array values. Additionally, it explains how to return arrays and structures from functions, highlighting the differences between passing by value and reference.

Uploaded by

Ahmad Raza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Tutorial 1

In this tutorial you will learn how to

 Pass an array to function by


o Value or Name
o Pointer

 Return an array from function by


o Pointer

 Pass Structure to function by


o Value
o Reference

 Return Structure from function by


o Value
o Reference

Tutorial | Array and Structure


Tutorial 2

To make this tutorial simple, we will use example of simple print


function and increment function (which will increment all values on
integer type array by 1).

Let’s see an example

#include <iostream>

using namespace std;

void printArray(int Arr[], int size) // See definition of function carefully


{
for (int i=0; i<size; i++) // Printing loop
cout<<Arr[i]<<endl; // Printing Array's Value one by one
}

int main()
{
int Array[] = {1,2,3,4,5}; // Declaring and Initializing an Array

printArray(Array, 5); // Passing Array by Name or Value

return 0;
}

In this example, we are passing an Array of integer type to a Print function


to print its values. Carefully see the declaration and arguments of the
function (important). In this way, we can pass Array to function by name or
value.

Now we will just change the arguments of the above function a little bit to
see another method of passing Array to function.

Tutorial | Array and Structure


Tutorial 3

#include <iostream>

using namespace std;

void printArray(int *Arr, int size) // See definition of function carefully


{
for (int i=0; i<size; i++) // Printing loop
cout<<Arr[i]<<endl; // Printing Array's Value
}

int main()
{
int Array[] = {1,2,3,4,5}; // Declaring and Initializing an Array

printArray(Array, 5); // Passing Array by Name or Value

return 0;
}

See the arguments of function. This is another method to pass Array to


function (by pointer). In this way we can pass any type of Arrays to any
function.

Now let’s see how we can return Array from a function.


#include <iostream>

using namespace std;

void printArray(int *Arr, int size) // See definition of function carefully


{
for (int i=0; i<size; i++) // Printing loop
cout<<Arr[i]<<endl; // Printing Array's Value
}

int * incrementArray(int Arr[], int size) // See definition of function


carefully
{
for (int i=0; i<size; i++)
Arr[i]++; // Incrementing Array's Value

Tutorial | Array and Structure


Tutorial 4

return Arr;
}

int main()
{
int Array[] = {1,2,3,4,5}; // Declaring and Initializing an Array

int * incrementedArray = incrementArray(Array, 5);

printArray(incrementedArray, 5); // Passing Array by Name or Value

return 0;
}

In this simple way we can return any type of array from any function.

Structure
Structure is Abstract data type or User defined data type. User make this
data type according to his requirement. Here we will see how we can
perform functions on structures. Is this look so simple? Yeah it is but most
of the students get confused in passing structure to function and
returning structure from a function.

Let’s see a simple example

#include <iostream>
#include <string>

using namespace std;

struct Student{ // Making a simple student structure

Tutorial | Array and Structure


Tutorial 5

string name;
string fatherName;
int age; // 3 members of structure
};

void printStructure(Student stu) // Passing structure by value


{
cout<<"Name of student : "<<stu.name<<endl;
cout<<"Father name of student : "<<stu.fatherName<<endl;
cout<<"Age of student : "<<stu.age<<endl;
}

int main()
{
Student Jawwad;

Jawwad.name = "Jawwad Saeed";


Jawwad.fatherName = "Saeed ur Rehman";
Jawwad.age = 21; // Assigning values to structure variables

printStructure(Jawwad);

return 0;
}

In this way we can pass A Structure to a function which will then perform
some specific task. We have a little problem here. As we know we are
passing structure by value so copy constructor will also called by
compiler here, also if we change anything in this function it will not effect
original values. How we can get rid of this?

It is very simple, let’s see an example

#include <iostream>
#include <string>

using namespace std;

struct Student{ // Making a simple student structure

string name;
string fatherName;
Tutorial | Array and Structure
Tutorial 6

int age; // 3 members of structure


};

void printStructure(Student &stu) // Passing structure by Reference


{
cout<<"Name of student : "<<stu.name<<endl;
cout<<"Father name of student : "<<stu.fatherName<<endl;
cout<<"Age of student : "<<stu.age<<endl;
}

int main()
{
Student Jawwad;

Jawwad.name = "Jawwad Saeed";


Jawwad.fatherName = "Saeed ur Rehman";
Jawwad.age = 21; // Assigning values to structure variables

printStructure(Jawwad);

return 0;
}

What I just did? I just passed structure to function by reference. In this


way we can change anything in function it will effect original values. There
is another way of doing that.

#include <iostream>
#include <string>

using namespace std;

struct Student{ // Making a simple student structure

string name;
string fatherName;
int age; // 3 members of structure
};

void printStructure(Student *stu) // Passing structure by value


{

Tutorial | Array and Structure


Tutorial 7

cout<<"Name of student : "<<stu->name<<endl; // must use arrow operator


here
cout<<"Father name of student : "<<stu->fatherName<<endl;
cout<<"Age of student : "<<stu->age<<endl;
}

int main()
{
Student Jawwad;

Jawwad.name = "Jawwad Saeed";


Jawwad.fatherName = "Saeed ur Rehman";
Jawwad.age = 21; // Assigning values to structure variables

printStructure(&Jawwad); // passing address of structure

return 0;
}

Now let’s see how we can return a structure from a function.


#include <iostream>
#include <string>

using namespace std;

struct Student{ // Making a simple student structure

string name;
string fatherName;
int age; // 3 members of structure
};

void printStructure(Student &stu) // Passing structure by value


{
cout<<"Name of student : "<<stu.name<<endl;
cout<<"Father name of student : "<<stu.fatherName<<endl;
cout<<"Age of student : "<<stu.age<<endl;
}

//This function will get values in passing object


//and return a student variable
Student assignReturnStructure(Student &stu)
{
cin>>stu.name;

Tutorial | Array and Structure


Tutorial 8

cin>>stu.fatherName;
cin>>stu.age;

return stu; // Returning stu object by value


}
//This function is returning object by value

int main()
{
Student student;

// copystudent will get values return by assignReturnStructure function


Student copystudent = assignReturnStructure(student);

printStructure(student);
printStructure(copystudent);

return 0;
}

In this way we can return an object by value. Now see how can we return
an object by reference.
#include <iostream>
#include <string>

using namespace std;

struct Student{ // Making a simple student structure

string name;
string fatherName;
int age; // 3 members of structure
};

void printStructure(Student &stu) // Passing structure by value


{
cout<<"Name of student : "<<stu.name<<endl;
cout<<"Father name of student : "<<stu.fatherName<<endl;
cout<<"Age of student : "<<stu.age<<endl;
}

//This function will get values in passing object


//and return a student variable by reference
Tutorial | Array and Structure
Tutorial 9

Student & assignReturnStructure(Student &stu)


{
cin>>stu.name;
cin>>stu.fatherName;
cin>>stu.age;

return stu; // Returing stu object


}
//This function is returning object by refernece

int main()
{
Student student;

// copystudent will get values return by assignReturnStructure function


Student copystudent = assignReturnStructure(student);

printStructure(student);
printStructure(copystudent);

return 0;
}

Tutorial | Array and Structure

You might also like