Basics of C++ programming

C++ Program Structure

• Before looking at how to write C++ programs consider the following simple example program.

Program Code Analysis

Line 1,2,3 of both program: // The First C++ program //prints Hello World! //Traditional Approach

Any text following the symbols // until the end of the line is ignored by the compiler. These are Comments in the C++ program- Single line comments.

• Comments also can be written in multiple line as in C between characters /* and */
Line 4 in first/2nd program: #include , #include
• Lines beginning with a hash sign (#) are command for preprocessor.
• They are special lines interpreted before the compilation of the program itself begins.
• Here in program, the directive #include , instructs the preprocessor to include a section of standard C++ code, known as header iostream, that allows to perform standard input and output operations, such as writing the output of this program (Hello World) to the screen.

Program Code Analysis

Line 5 in first /second program : int main ()
• All programs must have a function main( ).
• Note that the opening brace ({) marks the beginning of the body of the function, while the closing brace (}) indicates the end of the body of the function.
• The word int indicates that main() should return an integer value after execution.
• The main() function is a special function in all C++ programs; it is the function called when the program is run.
• The execution of all C++ programs begins with the main function, regardless of where the function is actually located within the code.
Lines 6 and 8 in first program and 7 and 9 in 2nd program: { and }
• Begin and End of main function
• Every statement written between { and } defines the main() function body.

Line 7 in First Program : cout<<"Hello World! ";
• This is a C++ statement that actually sends the string “Hello World!” to the output device(scree) to print.
Line 5 in 2nd C++ Program: using namespace std; • A standard namespace that defines the scope of inpput/output object for output
• We can write std::cout<<“Hello World!” if this line is not included. It calls cout from std namespace.
• With using namespace, whole namespace can be used without operator ::.

Variable Definition at the point of use

• In C++, local variables can be created at any required position in the code, even between statements.
• Also local variables can be defined in some statements, just before their usage.
• Find below an example: //prog3.cpp

C++ Program Structure

• Before looking at how to write C++ programs consider the following simple example program.
// Sample program to read the length and width of a rectangle and returns the perimeter and area of the rectangle. /*prog5*/

Fundamental Data Types in C++

Type deduction: auto and decltype

• When a new variable is initialized, the compiler can figure out what the type of the variable is automatically by the initializer.
• For this, it can be used auto as the type specifier for the variable:
int var1 = 0;
auto var2 = var1; // the same as: int var2= var1;
• Here, var2 is declared as having an auto type; therefore, the type of var2 is the type of the value used to initialize it
• in this case it uses the type of var1, which is int.
• Variables that are not initialized can also make use of type deduction with the decltype specifier:
int var1= 0;
decltype(var1) var2;
// the same as: int var2;

Input and Output:

• C++’s new features for handling I/O operations are called streams.
• Streams are abstractions that refer to data flow.
• Streams in C++ are :
– Output Stream
– Input Stream.
• The output stream allows us to write operations on output devices such as screen, disk etc.
• Output on the standard stream is performed using the cout object.
• C++ uses the bit-wise-left-shift operator for performing console output operation.
• The syntax for the standard output stream operation is as follows: – cout<• The word cout is followed by the symbol << , called the insertion or put to operator , and then with the items (variables, constants, expressions) that are to be put into screen.
• The us of cout to perform an output operation is as shown :

Output stream

• The following are example of stream output operations:
cout << “hello world”;
int age;
cout<< age;
float weight; cout<< weight;
• Output operations in C++ can be cascaded with single cout with multiple variable or constant. For example
cout<<“Age is:” <cout<<“Age is: “<

Input Stream

• The input stream allows us to perform read operations with input devices such as keyboard, disk etc.
• Input from the standard stream is performed using the cin object.
• C++ uses the bit-wise right-shift operator for performing console input operation.
• The syntax for the standard output stream operation is as follows: cin<• The word cin is followed by the symbol >> and then with variable, into which input data is to be stored.
• The use of cin to perform an input operation is as shown :
• Following Examples show the stream input operations;
int amount;
cin>>amount;
float weight;
cin>>weight;
char name[20];
cin>>name;
• Similar to output, input operation in C++ are can also be cascaded.
• For example, reading the name of a person his address, age can be performed by the cin as:
cin>> name>>address>>age;

Manipulators

• Manipulators are instructions to the output stream that modify the output in various ways. For example endl , setw etc. The endl Manipulator:
• The endl manipulator causes a linefeed to be inserted into the stream, so that subsequent text is displayed on the next line.
• It has same effect as sending the ‘\n’ character but somewhat different.
• It is a manipulator that sends a newline to the stream and flushes the stream.
• Unlike ‘\n’ it also causes the output buffer to be flushed but this happens invisibly.
cout << "Perimeter is " << perimeter<cout << endl << "Area is " << area << endl;
Output of above 2 lines:
Perimeter is 36
Area is 80
• Manipulators come in two flavors: those that take and arguments and those that don’t take arguments.
• Followings are the some important non-argument manipulators .
• Table: No-argument Manipulators

The setw Manupulator

• To use setw manipulator the “iomanip.h” header file must be included.
• The setw manipulator causes the number or string that follows it in the stream to be printed within a field n characters wide , where n is the argument used with setw as stew(n).
• The value is right justified within the field.
• e.g. //demonstrates setw manipulator

Scope Resolution Operator (::)

• C++ supports a mechanism to access a global variable from a function in which a local variable is defined with the same name as a global variable. It is achieved using the scope resolution operator.
Syntax for accessing Global Variable:-
:: GloabalVariableName
• The global variable to be accessed must be preceded by the scope resolution operator.
• The scope resolution operator permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.
• //An example of use of scope resolution operator ::

Dynamic Memory Allocation(DMA)

• In the programs described in previous slides, all memory needs for variable were determined before program execution by defining the variables needed.
• But there may be cases where the memory needs of a program can only be determined during runtime.
• When the memory needed depends on user input, programs need to dynamically allocate memory.
• For this mechanism the C++ language integrates the operators new and delete for which malloc() , calloc(), realloc(), free() functions used in C.

DMA- new and delete operators

• Memory is allocated in C++ using operator new.
• new is followed by a data type specifier.
• If memory for a sequence of more than one element is required, number of these is placed within brackets [].
• new returns a pointer to the beginning of the new block of memory allocated. Its syntax is:
pointer = new type;
pointer = new type[number_of_elements];
• The first expression is used to allocate memory to contain one single element of type type.
• The second one is used to allocate a block (an array) of elements of type type, where number_of_elements is an integer value

DMA- new Example

int *a;
a=new int;
• In in above line system allocates memory for an integer and assigns the address of allocated memory to variable a.
int *x;
x=new int[5];
• In this above case, the system dynamically allocates space for five elements of type int and returns a pointer to the first element of the sequence, which is assigned to variable the x (a pointer).
• Therefore, x now points to a valid block of memory with space for five elements of type int.
• Here x is pointer, thus, the first element point by x is accessed by x[0] or *x or *(x+1) , second element by x[1] or *(x+1) and so on
• C++ provides two standard mechanisms to check if the allocation was successful
• One is by handling exceptions. Using this method, an exception of type bad_alloc is thrown when the allocation fails.
• Exceptions are a powerful C++ feature explained later in this course.
• But for now, you should know that if this exception is thrown and it is not handled by a specific handler, the program execution is terminated.
• This exception method is the method used by default by new, and is the one used in a declaration like below:
int
*ptr = new int [5];
// if allocation fails, an exception is thrown
• The other method to check whether the allocation is successful or not is known as nothrow.
• This method can be specified by using a special object called nothrow, declared in header as argument for new.
• Using notthrow, instead of throwing bad_alloc exception or terminating the program, the pointer returned by new is a null pointer and the program continues its execution normally.
int *ptr = new (nothrow) int [5];
• In this case, if the allocation of this block of memory fails, the failure can be detected by checking if ptr is a null pointer:
int * ptr;
ptr = new (nothrow) int [5];
if (ptr == nullptr)
{
// error assigning memory.
}

Memory Deallocaton- delete and delete[]

• In most cases, memory allocated dynamically is only needed during specific periods of time within a program.
• once it is no longer needed, it can be freed so that the memory becomes available again for other requests of dynamic memory.
• This is the purpose of operator delete, whose syntax is:
delete pointer;
delete[] pointer;
• The first statement releases the memory of a single element allocated using new, and the second one releases the memory allocated for arrays of elements using new and a size in brackets [].
• The value passed as argument to delete shall be a pointer to a memory location or memory block previously allocated with new

DMA Example with new and delete

Reference Variables

• A reference variable provides an alias (Alternative name) of the variable that is previously defined.
• For example, if we make the variable sum a reference to the variable total, the sum and total can be used interchangeably to represent the same variable.
• Syntax for defining reference variable: data_type &ref_name = variable_name;
• Example:
int total=100 ;
// Here total is int type variable declared first
int &sum=total;
// sum is the alias for variable total.
• Both the variable refer to the same data 100 in the memory location.
• cout<cout<//gives output 200
• A reference variable must be initialized at the time of declaration. This establishes the correspondence between the reference and the data object which it means. The initialization of reference variable is completely different from assignment to it.
• A major application of the reference variables is in passing arguments to function by reference.

An example of reference

Passing by reference

• In C++ ,we can pass parameters in function by reference .When we pass arguments by reference, the formal arguments in the called function become aliases to the actual arguments in the calling function
• When function is working with its own arguments , it is actually working on the original data.

Return by reference

• A function can return a value by reference. This is a unique feature of C++.
• Normally function is invoked only on the right hand side of the equal sign. But we can use it on the left side of equal sign and the value returned is put on the right side.
//returning by reference from a function as a parameter

Inline Function

• Calling a function generally causes a certain overhead like stacking arguments, jumps, etc.
• So for very short functions, it may be more efficient to simply insert the code of the function where it is called, instead of performing the process of formally calling a function.
• So make function inline so that compiler inserts the function code at the place of call during compilation –called inline compilation
• A inline function is a function written before the main function starting with keyword inline followed by return type of function.
• The syntax of inline function is:
• In ordinary functions, when function is invoked the control is passed to the calling function and after executing the function the control is passed back to the calling program.
• But , when inline function is called, the inline code of the function is inserted at the place of call and compiled with the other source code together.
• That is the main feature of inline function and different from the ordinary function.
• So using inline function executing time is reduced because there is no transfer and return back to control.
• But if function has long code inline function is not suitable because it increases the length of source code due to inline compilation.
• For large coded function, inline implementation increases the storage overhead

Inline Function Example

Function with Default Arguement

• In C++, functions can also have optional parameters, for which no arguments are required in the call.
• For an example, a function with three parameters may be called with only two or one or none.
• For this, the function shall include a default value for its parameters, which is used by the function when called with fewer arguments.

Namespaces and Using directive

• Named entities, such as variables, functions, and compound types need to be declared before being used in C++.
• The point in the program where this declaration happens
influences its visibility or called scope of that entity • An entity declared outside main(){…} block has global scope, meaning that its name is valid anywhere in the code and also called the global variables.
• While an entity declared within a block, such as a function or a selective statement or any blocked enclosed by braces { …} has block scope, and is only visible within the specific block in which it is declared, but not outside it.
• Variables with block scope are known as local variables.

Namespace

• Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.
• The format of namespaces is:
namespace identifier {
entities
}
• Where identifier is any valid identifier and entities is the set of classes, objects and functions that are included within the namespace. For example:
namespace myNamespace
// myNamespace is name of namespace
{
int a, b;
}
• Here a and b are integer variables declared in myNamespace namespace myNamespace
// myNamespace is name of namespace
{
int a, b;
}
• In order to access these variables from outside the myNamespace namespace we have to use the scope operator ::.
• For example, to access the previous variables from outside myNamespace we can write:
myNamespace::a
myNamespace::b

using directive

Namespace alias

• We can declare alternate names for existing namespaces according to the following format:
namespace new_name = current_name;
Namespace std
• All the files in the C++ standard library declare all of its entities within the std namespace.
• That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.
• Without using namespace std; statement, the call of cout , cin can be done as :
std::cout<<“Hello world”;
std::cin>>variable;

Const Arguments

• When arguments are passed by reference to the function, the function can modify the variables in the calling program.
• Using the constant arguments in the function , the variables in the calling program can not be modified. const qualifier is used for it.
e.g.
void func(int&,const int&);
int main()
{
int a=10, b=20;
func(a,b);
}
void func(int &x, int &y)
{
x=100; //Valid
y=200; // error since y is constant argument
}

Function overloading

• Overloading refers to the use of same entity for different purpose.
• When same function name is used for different tasks this is known as function overloading.
• Function overloading is one of the important feature of C++ and any other OO languages.
• When an overloaded function is called the function with matching arguments and return type is invoked.
e.g.
1. void char_line(); //function with no arguments
2. void char_line(int ); // function with one int argument
3. void char_line(char); //function with one float argument
4. void char_line(int,char); // function with one int and one
// float arguments

Function Overloading Example

Pointers

• A PC's RAM consists of many thousands of sequential storage locations, and each location is identified by a unique address.
• The memory addresses in a given computer range from 0 to a maximum value that depends on the amount of memory installed.
• For a C++ program, the memory of a computer is like a succession of memory cells, each one byte in size, and each with a unique address.
• These single-byte memory cells are ordered in a way that allows data representations larger than one byte to occupy memory cells that have consecutive addresses.
• This way, each cell can be easily located in the memory by means of its unique address.
• For example, the memory cell with the address 1776 always follows immediately after the cell with address 1775 and precedes the one with 1777, and is exactly one thousand cells after 776 and exactly one thousand cells before 2776.
• When a variable is declared, the memory needed to store its value is assigned a specific location in memory (its memory address).
• Generally, C++ programs do not actively decide the exact memory addresses where its variables are stored.
• The task of memory allocation is left to the environment where the program is run - generally, an operating system that decides the particular memory locations on runtime.
• However, it may be useful for a program to be able to obtain the address of a variable during runtime in order to access data cells that are at a certain position relative to it.

What is a Pointer?

• A Pointer is a variable which contains address of other variable.
Pointer Operators:
• The ‘&’ - address-of operator, is a unary operator that returns the address of its operand.
• The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), the address-of operator. For example:
– x= &y; // here x must be pointer and &y represents address of y.
– Here, address of y has been assigned to pointer variable x.
• The ‘*’ operator, commonly referred as Dereference operator, returns the value of the object to which its operand ( a pointer) points.

Pointer and Operator- & operator

• The actual address of a variable in memory cannot be known before runtime, but let's assume, in order to help clarify some concepts, that a variable my_var1 is placed during runtime in the memory address 1776.
• In this case, consider the following code fragment:
my_var1 = 25;
ptr = &my_var;
Var = my_var;
• The values contained in each variable after the execution of this are shown in the adjacent diagram:

Pointer and Operator- * operator

• An interesting property of pointers is that they can be used to access the variable they point to directly.
• This is done by preceding the pointer name with the dereference operator (*).
• The operator itself can be read as "value pointed to by".
• Look at the example in next slide:
Look at the previous example as:
my_var1 = 25;
ptr = &my_var; // address of my_var is assigned to ptr so ptr points my_var
var = *ptr; // var gets assigned with value of variable pointed by pointer ptr.
• var = *ptr could be read as: “var equal to value pointed to by ptr", and the statement would actually assign the value 25 to var, since ptr is 1776, and the value pointed to by 1776 would be 25. Look at the picture below

Declaring Pointers/ Assigning address

• A pointer is declared using ‘*’ operator(dereference operator) as follows:
type_name *variable_name;
e.g.
int x; // a integer variable
int *y; // a integer pointer variable
• Here x is a variable declared as int and y is a pointer variable which can be assigned the address of variable declared as int. We can also say that y is a pointer to int.
• A pointer can be declared as any data type. A pointer declared as one type can be assigned the address of variable declared as same type only.
• It will be wrong to assign address of one type variable to other type pointer.
• To assign the address of a variable to pointer, we use ‘&’ operator(address-of) .
• Example:
int a, *b;
float c, *d;
double e, *f;
b = &a ; // assigns the address of a to pointer b
d = &c ; //assigns the address of c to ptr d
f = &e ; //assigns the address of e to ptr f
• The address-of operator ‘&’ only applies to the variables and array elements. It cannot be applied to the expressions, constants or register variables.

Using * and &

• As described above slide, & is used to represent the address of a variable and * is used to declare the pointer variable and to access the value of variable pointed by the pointer variable
Example:
int a, *b; // declaration of an integer variable ‘a’ and a pointer to integer ‘b’
a = 10; // a has assigned value 10
b = &a; // pointer variable b has assigned the address-of variable a , so b points a.
• Now , *b points to value of ‘a’ i.e. we can access value of a by using b as *b.
For example: *b =12; results the value of ‘a’ equal to 12;
• Since pointer are also variables they can also be used without indirection operator ‘*’ to assign address from one pointer to another pointer variable.
• Example:
int a; // declaration of variable ‘a’ of type int.
int *b, *c; // declaration of two pointer variables b and c of type int.
a =10; // assigns the value 10 to variable ‘a’.
b= &a; // assigns the address of ‘a’ to pointer ‘b’ i.e. b points to ‘a’.
• Now, c = b; assigns the content of ‘b’ to ‘c’ i.e. address of ‘a’ to ‘c’.
• So the pointers ‘c’ and ‘b’ both now points to a and *b and *c both represent the value 10.

Pointer and Arrays

• The relationship between arrays and pointers is very strong.
• So , arrays and pointers can be used interchangeably in C/C++ and discussed simultaneously.
• The array name always points to the first element of that array.
• For example , here is an array a declared as int which can store 10 integers as
int a[10];
Then, array name a points to a[0]; i.e. a and &a[0] refer same. So we can say a is the pointer to a[0], the first element in the array a itself.
• Look at the output above,
– Value of a[i], *(a+i) , b[i] and *(b+i) is same for all i =0 to 4
– Similarly value of (a+i) and (b+i) is same for each i=0 to 4

Pointer Algebra

RULE 1:
• If ‘*’(dereference operator) and ‘&’ (address-of operator) placed together , they cancel each other i. e. meaning less.
Example:
Let
m = &n; // assign address of n to ptr m.
Then look at statement,
*m = *&n ;
• As *& cancel each other, *m = n.
• So once we say m =&n, then, *m and n are identical and refer to the same variable i. e. *m refers the value pointed by m i.e. Value of n.
RULE 2:
• Let ‘m’ be declared as an array or as pointer then
–&m[i] is equivalent to (m+i)
–m[i] is equivalent to *(m+i)
• In the above program,
• b=&a[0]; assigns the address of first element of array to the pointer ‘b’.
• Also b = a ; is same as ‘b= &a[0];’
• To fetch the value of first element we can write: a[0] or *b or *(a+0) or *a
• Similarly for next element we can write:
• a[1] or *(b+1) or *(a+1) and &a[1] is same as b+1
• a[2] or *(b+2) or *(a=2) and &a[2] is same as b+2 … and so on
• In general, a[i] is same as *(b+i) , i= 1,2,3,4,…. And &a[i] is same as b+i

Pointer Algebra: Multidimensional Array and Pointer

RULE #3
• Let m is declared as array then
– m[i][j] ➔ *(*(m+i)+j); // two dimensional
– m[i][j][k] ➔ *(*(*(m+i)+j)+k) // three dimensional
– m[i][j][k][l] ➔ *(*(*(*(m+i)+j)+k)+l) and so on..
– • We can access any element of a multidimensional array using pointer. Suppose the declaration of an array like:
• int m[20][20];
• Now , we want to fetch the element m[10][8]. Fetching this element from array using pointer can be done as:
• Let us substitute m[10] with x.
– m[10][5] ➔ x[5] on substituting m[10] with x
– ➔ *(x+5) as per the Rule 2 of pointer Algebra
– ➔ *(m[10]+5) on re-substituting x with m[10].
– ➔ *(*(m+10)+5) on substituting m[10] with *(m+10) as per rule 2 of Pointer Algebra
• Hence the value of m[10][5] can be fetched using array name as pointer by using
• *(*(m+10)+5) which is equivalent to m[10][5].
• If we use array and pointer together , we can fetch the value of m[10][5] by using any of the following.
• m[10][5] ➔ *(*(m+10)+5) , In general m[i][j] ➔ *(*(m+i)+j)

Are arrays and pointer same ?

• Although, array name can be used as pointer and pointer name can be used as array, but arrays and pointers are not same they can be only used interchangeably to represent same things.
• Followings are the differences between arrays and pointers:
• When an array is declared like
int a[10];
• Compiler allocates storage for 10 integers where as we declare i.e. 10*2 =20 bytes( assume 2 bytes for integer)
• When we declare a pointer like int *b;
• Storage is allocated for only one pointer i.e. only 4 bytes of memory for pointer b. To allocate storage for array that is to be pointed by b, we have to use new operator as,
b=new int[10];
• A pointer is a variable so we can write:
b = b+2; etc
• where as array name is not a variable so it is not valid to write a = a + 2;

Pointer Arithmetic

• To conduct arithmetical operations on pointers is a little different than to conduct them on regular integer arithmetic.
• Only addition and subtraction operations are allowed where the other arithmetic operations make no sense in the world of pointers.
• But both addition and subtraction have a slightly different behavior with pointers, according to the size of the data type to which they point.
• When fundamental data types were introduced, we saw that types have different sizes.
• For example: char always has a size of 1 byte, short is generally larger than that, and int and long are even larger; the exact size of these being dependent on the system.
• For example, let's imagine that in a given system, char takes 1 byte, short takes 2 bytes, and long takes 4 bytes.
• Suppose now that we define three pointers in this compiler as:
char *mychar;
short *myshort;
long *mylong;
And let we know that they point to the memory locations 1000, 2000, and 3000, respectively. Therefore, if we write:
++mychar;
++myshort;
++mylong;
• mychar, will contain the value 1001. But not so obviously for myshort and mylong.
• myshort would contain the value 2002, and mylong would contain 3004, even though they have each been incremented only once.
• The reason is that, when adding one to a pointer, the pointer is made to point to the following element of the same type.
• Therefore, the size in bytes of the type it points to is added to the pointer.

Pointers to pointers

• C++ allows the use of pointers that point to pointers, that , in its turn, point to data (or even to other pointers).
• The syntax simply requires an asterisk (*) for each level of indirection in the declaration of the pointer:
char a; // declares an int variable a.
char * b; // declares a pointer b for int pointer
char ** c; // declares an int pointer to pointer c
a = 'z'; // assigns a character ‘z’ to variable a.
b = &a; // assigns address of a to pointer b
c = &b; // assigns address of pointer b to pointer to pointer variable c.
• The below picture shows assuming the randomly chosen memory locations for each variable of 7230, 8092, and 10502, could be represented in memory as :
• The new thing in this example is variable c, which is a pointer to a pointer, and can be used in three different levels of indirection, each one of them would correspond to a different value:

Function and Pointers

• A function is not a variable but it is allowed to define pointer to a function like a variable. The function pointer is declared and used as mentioned below.
• Let a function square(int) used to compute and return the square of an integer number defined as
int squqre(int n)
{
return n*n;
}
• the pointer to function is declared as, data type (*ptrname)(parameter type);

Function Pointer

e.g. int (*fptr)(int); Note that the parentheses around *fptr is essential.
• Without parentheses,
int *fptr(int);
means: fptr is a function returning a pointer to integer which is different from pointer to function.
• Assigning the address of function square() to function pointer *fptr is done as
fptr =square; // just name of function only.
• The function name itself represents the address of the function.
• The function is invoked by using pointer to function as:
• Sq = (*fptr)(n); // where n is any integer value and sq is a integer variable. This statement is equivalent to Sq= square(n);
The function pointer can be used as function arguments. Following example shows about this concept.

Structure review

• A structure is an user defined data type which contains the collection of different types of data under the same name(Heterogeneous Data structure)
• A structure is compared with records in other languages.
Syntax:
struct tag // tag is name given to structure - an identifier, struct is keyword
{
data-type var-name;
data-type var-name;
…………………..
} ; //end of structure
e.g. struct currency // currency is tag or name
{
int rs;
float ps;
};
• declaring variable of structure
tag st_var_name; // st_var_name is variable declared of struct tag
e.g. currency c1,c2; // currency is structure name i.e. user defined data type and c1 and c2 are
//variables of currency type

Initialization of structure variable:


• Structure variable can be initialized at the time of declaraction as
currency c1 ={ 144,56.7}; // structure in previous slide
• However each member data of structure variable can be initialized separately after declaration as
currency c1;
c1.rs=144;
c1.ps=56.7;
• But initialization all member data at once listing is illegal after declaration as
currency c1;
c1={144,56.7} // error:

Example of Structure

Passing structure as function arguments

• A function can receive the structure as parameter and is able to access and operate on the individual elements of the structure.
• When passing the structure variable as the function argument , we can use either Passing by value, Passing by address or Passing by reference as defined by function.
• The following examples shows the passing structure as function argument in different ways.

0 Comments