Background and HW/SW required for this course.
Explore C++ Key Course Features
Make sure you have the background and equipment required for this course.
Introduction to C++ is intended for experienced C programmers who are already comfortable writing C programs that use pointers,
arrays, functions, and structures.
Pass Structures to functions
We can pass structures to functions.
Structures are passed into functions as per the C/C++ calling conventions by value. In other words, a copy of entire structure is put on the stack.
The function is called which removes it from the stack and uses the structure. We can also pass the structures by reference to function.
This can be performed in the same way we do with the normal variables i.e. pass the address of the structure to the function. This is call by reference.
When we pass an array to a function, the reference of the array is passed to the function.
Any change in the array elements changes the original array. Suppose we have a structure containing an array.
What will happen to the array if the structures are passed as value?
Question: Is the array passed as value or reference?
As the array is a part of structure, it will be passed as value.
The advantage of pass by value process is that if the function makes some changes to the array elements, it does not affect the original array.
However, it may be disadvantageous as the complete array is copied on the stack and we can run out of memory space. So be careful while passing the structures to functions.
We know that functions return value, int, char etc.
Similarly functions can also return structures. In a way, the behavior of structure is same as ordinary data type.
Structures and Pointers
Suppose we have a pointer to structure as student *sptr; here sptr is a pointer to student. Now s1 is a variable of type student and sptr = &s1 and sptr is pointing to s1.
How can we access the data with sptr? We cannot say *sptr.name.
The precedence of dot operator (.) is higher than * operator. So dot operator is evaluated first and then * operator.
The compiler will give error on the above statement.
To get the results, we have to evaluate * operator first i.e. (*sptr).name will give the desired result.
There is another easy and short way to access the data member of a structure and that is using the arrow (->) in place of the
We normally use the arrow (-> i.e. minus sign and then the greater than sign) to manipulate the data structure with pointers. So to access the name with sptr we will write:
Arrays of Structure
The declaration is similar as used to deal with the simple variables. The declaration of array of hundred students is as follows:
In the above statement, s is an array of type student structure.
The size of the array is hundred and the index will be from 0 to 99. If we have to access the name of first student, the first element of the array will be as under:
Here s is the array so the index belongs to s.
Therefore the first student is s, the 2nd student is s and so on.
To access the data members of the structure, the dot operator is used. Remember that the array index is used with the array name and not with the data member of the structure.
You can use either Windows, Mac OSX, or Unix machines to take this course. The course is intended to be platform-independent. Please be
sure you are aware of platform-dependent issues that may affect your compiler.