Selasa, 24 November 2015

Pointer And Reference

What is Pointer?

pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration:


type *var-name;

Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Following are the valid pointer declaration:

int         /* pointer to an integer */
double   /* pointer to a double */
float      /* pointer to a float */
char      /* pointer to a character */

As you know, every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory. Consider the following example, which prints the address of the variables defined :

#include <stdio.h>

int main () {

   int  var1;
   char var2[10];

   printf("Address of var1 variable: %x\n", &var1  );
   printf("Address of var2 variable: %x\n", &var2  );

   return 0;
}

When the above code is compiled and executed, it produces the following result 

Address of var2 variable: bff5a3f6

What Are Pointer And Reference?
Pointers and reference are essentially variables that hold memory addresses as their values. You learned before about the various different data types such as: intdouble, and char. Pointers and references hold the addresses in memory of where you find the data of the various data types that you have declared and assigned. The two mechanisms, pointers and references, have different syntax and different traditional uses.

Declaring Pointer And Reference


When declaring a pointer to an object or data type, you basically follow the same rules of declaring variables and data types that you have been using, only now, to declare a pointer of SOMETYPE, you tack on an asterix * between the data type and its variable.

SOMETYPE* sometype;  int* x; 

To declare a reference, you do the exact same thing you did to declare a pointer, only this time, rather than using an asterix *, use instead an ampersand &.

SOMETYPE& sometype; 

int& x;

As you probably have already learned, spacing in C++ does not matter, so the following pointer declarations are identical:

SOMETYPE*  sometype;
SOMETYPE * sometype;
SOMETYPE  *sometype;

The following reference declarations are identical as well:

SOMETYPE&  sometype;
SOMETYPE & sometype;
SOMETYPE  &sometype;


The Null Pointer

Remember how you can assign a character or string to be null? If you don't remember, check out HERE. The null character in a string denotes the end of a string, however, if a pointer were to be assigned to the null pointer, it points to nothing. The null pointer is often denoted by 0 or null. The null pointer is often used in conditions and/or in logical operations.


#include <stdio.h>

int main () {

   int  *ptr = NULL;

   printf("The value of ptr is : %x\n", ptr  );
 
   return 0;
}


When the above code is compiled and executed, it produces the following result 
The value of ptr is 0

If pointer px is NOT null, then it is pointing to something, however, if the pointer is null, then it is pointing to nothing. The null pointer becomes very useful when you must test the state of a pointer, whether it has a value or not.

To check for a null pointer, you can use an 'if' statement as follows
if(ptr)     /* succeeds if p is not null */
if(!ptr)    /* succeeds if p is null */

Returning Pointers and References from Functions

When declaring a function, you must declare it in terms of the type that it will return, for example:


int MyFunc(); // returns an int 
SOMETYPE MyFunc(); // returns a SOMETYPE 

int* MyFunc(); // returns a pointer to an int 
SOMETYPE *MyFunc(); // returns a pointer to a SOMETYPE 
SOMETYPE &MyFunc(); // returns a reference to a SOMETYPE 

The declaration of a function that returns a pointer or a reference should seem relatively logical. The above piece of code shows how to basically declare a function that will return a reference or a pointer.


SOMETYPE *MyFunc(int *p) 
{ 
   ... 
   ... 
   return p; 
} 

SOMETYPE &MyFunc(int &r) 
{ 
  ... 
  ... 
  return r; 
}

Within the body of the function, the return statement should NOT return a pointer or a reference that has the address in memory of a local variable that was declared within the function, else, as soon as the function exits, all local variables ar destroyed and your pointer or reference will be pointing to some place in memory that you really do not care about. Having a dangling pointer like that is quite inefficient and dangerous outside of your function.
However, within the body of your function, if your pointer or reference has the address in memory of a data type, struct, or class that you dynamically allocated the memory for, using the new operator, then returning said pointer or reference would be reasonable.


SOMETYPE *MyFunc()  //returning a pointer that has a dynamically 
{           //allocated memory address is proper code 
   int *p = new int[5]; 
   ... 
   ... 
   return p; 
} 


Returning Pointers to an Array

you can return a pointer to an array by specifying the array's name without an index.If you want to return a single-dimension array from a function, you would have to declare a function returning a pointer as in the following example:

int * myFunction()
{
.
.
.
}

Tidak ada komentar:

Posting Komentar