Professional Documents
Culture Documents
Week 5
Week 5
•Pointer to a Function
•Structures
• REF: . Kunal Pimparkhede - Computer Programming with C++ (2016, Cambridge University Press).
void Pointers
Void type pointers can point to a variable of any type. void pointers are universal
pointers so they have ability to point to data values of every type.
The code below creates two variables a and b of type int and float. It uses a void type
pointer p to access the value of each of these variables.
will make the void type pointer p to point to floating point variable a.
to copy the address stored in pointer p to pointer pint
We can access the values of variables a and b by applying indirection to pointers pint
and pflt as shown in the code.
Pointer to a Function
Pointers in C/C++ can also point to a function. To make a pointer point to a function,
type of the pointer must match with the prototype of the function.
Consider the given program, add() and sub() functions take two integer values as an
argument and return an integer as a result of execution. Since both of the function
have a same prototype, it is possible to create a single pointer which has an ability to
point to either of the functions.
Reference Variables (Only Available in C++ not in C)
A reference variable in C++ is another name given to a variable. we can give multiple
names to the same memory location by creating multiple references to the same location
Both statements will print a value 10 which is the
value stored in the memory location which has
two names a and sample.
Call by reference
When a function is called by passing the name of the actual parameters as argument
instead of passing the values, the function call is then defined as a ‘call by reference’
Call by reference Execution of the statement p++
Creating Lvalue’d function using reference variables
(Possible only in C++ and not in C)
Reference variables and pointers both can be used to create Lvalue’d function.
The first statement in main(), f1()=20; attempts to call a function f1() as a Lvalue
(The function call is placed on the left-hand side of assignment operator)
• As per the function definition of f1() it increments the value of a and
returns the resultant value. So, the function f1() returns 11.
• the function returns the value of a and not the memory location
where a is stored.
• Hence the program crashes throwing Lvalue required. To make this logic work, you
must rewrite the statement as k=50-1; This will work perfectly fine because left-
hand side of assignment is a simple variable. Hence the statement will set the value
of k as 49.
Structures
• Each of the data elements defined inside the structure, are called the
members of a structure.
Syntax of a structure
• Once the structure is defined, we can now create variables of type structure
similar to how we create variables of any of the basic data types such as int,
float, and char.
• The variables which are of type defined by the structure are called as
objects of the structure. The following is the syntax to create objects of a
structure:
Object memory for o1 and o2
The total memory required by any object will be the addition of the amount of
memory required by each of its members.
• We can similarly, create multiple variables of type Book and each variable will
contain all the members of the structure
The variable b1 is now called as an ‘object’ of a structure Book. The object memory
of b1 will contain members id, title, author and price
Object memory of b1
In C++, you can also create an object b1 by omitting keyword struct as shown in
the statement below:
Book b1;
As we need to store data about three books, we create two more variables of
type Book by using the following statement:
Initialization can only be applied at the time of creation of the object and not later than
that,
Can we initialize the members of the structure within the structure definition?
NO! This is because the memory for members is not allocated until an object of a
structure is created
Program to store the information about books in the computer memory: An
example
Create a program that stores the data about three books in a computer memory
and prints the information about each of the Book objects in a tabular format.
Notes In this case we have created members title and author as arrays rather
than pointers. This is because we intend to accept this information as input from
the user rather than directly initializing them as string constants
Memory allocation of a structure object with arrays embedded within the object space
As we need to store the information about three books in computer memory, the
following code creates three objects of a Book taking the details of each Book object as
input from the user and printing them back on screen.
State of objects b1, b2 and b3
SHORTHAND NOTATION TO CREATE OBJECTS
It is possible to create objects of a structure by specifying a comma separated list
of object names along with the structure definition.
Since structure Book is defined globally the objects b1, b2 and b3 will also be
created as global objects. On the other hand, if b1, b2 and b3 are created in main()
using the statement
the objects will be local to main() function even if the structure Book is defined
globally
Syntax for creating objects of a structure using shorthand notation
Array of Structure Objects
It is possible to create an array such that each element of the array is an object of
structure. Let us consider the structure Book; We can create an array of Book
objects named as b by using
Each element of the array b is an object of Book, because the data type of the array
b is defined as Book.
Array of objects b
For example, the following statement will accept the members (id, title, author, and
price) of object b[0] as input from the user:
Since we have only taken the members of b[0] as input, the state of array b assuming
input data as (id=1, title="C++", author="Kunal", price=900)
Initialization of objects
for(i=0;i<=2;i++)
{
scanf("%d%s%s%f",&b[i].id,b[i].title,b[i].author,&b[i].price);
}
In general, we can access a member of ith Book object in b by using the following
statement:
Following for loop will print the details of each of the Book objects on the computer
screen:
For example, the following code will not work because it first creates an array b
and then initializes all its objects in the second statement.
If you need to initialize members of an array after its creation, you have to initialize
them one at a time as shown below