Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me understand these with a thorough easy-for-beginners-to-understand

ID: 3570139 • Letter: P

Question

Please help me understand these with a thorough easy-for-beginners-to-understand explanation. Thanks :)

How do i know when i need to use/declare a pointer? Why does the asterisk sometimes come before the variable name and sometimes after? When declaring a pointer do you put the asterisk with the variable name or with the type? For example, would you have string* pointer or would you do string *pointer instead?

What is the flush function for? How is it useful?

What is the purpose of the member access operator (->) and when should it be used?

Explanation / Answer

a)

A pointer is nothing by referencing to a particular variable memory address or to a variable that holds the address of the memory address.

This is represented by a symbol as asterisk(*). By looking the asterisk itself it is clear that it is pointer.

When you work with a variable, the size of the allocated to the variable may not be predicted at times. To refer to the variable memory address and to access the variable, the pointer comes into picture.

For example,

            int x[20];

            int *p=x;

Here, in the above code, x is an array that contains set of integers. The address of the array of x is stored in a readable form in the integer pointer p.

Usage:

For example, consider a library. When you want to refer of get a book, you first go to particular subject book rack where the particular subject name is specified. Then you search for the particular book by its name.

Here book is the value, rack is huge memory, and name of the book is place of the memory. Here the rack number or name is referred to be the pointer.

b)

In general, both the ways it refers to be same meaning.

For example,

            int *a;

the above statement indicates, a is a pointer variable which refers to integer type.

            int * a;

the above statement indicates, integer is pointer which is referenced by a variable a.

At some cases, the meaning would be changed. For example,

            int* x, y, z;

Here, x, y and z are variables of type integers. But whereas, y and z are normal integer variables and x is pointer to the integer type.

            int *x, *y, *z;

Here, if observed, all the three variables are of pointers of integer type.

c)

When declaring a pointer do you put the asterisk with the variable name or with the type?

In both the ways, the asterisk can be placed when declaring a single pointer. As shown above examples.

But, it is always preferable to put the asterisk with the variable name.

In other words, it is left to programmer choice.

d)

It is preferable to declare

string *pointer.

e)

Flush function is to synchronize the stream buffer which is associated with the output sequence controllers.

By using output sequences, it updates the data to the respective output systems. This is basically used in the file systems.

For example,

            ofstream ofile(“newText.txt”);

     if(ofile)

     {

          for(int i=0;i<100;i++)

          {

          ofile<<i;

          ofile.flush();

}

}

The above code, updates the text file with the value of ‘i’ continuously for 100 times.

f)

The purpose of the operator (->) is that, it references to the pointer variable object. When it is used, it should return either a pointer or object of the structure or class object pointer.

The scope to the operator is with in the class itself.