By clicking “Check Writers’ Offers”, you agree to our terms of service and privacy policy. We’ll occasionally send you promo and account related email
No need to pay just yet!
About this sample
About this sample
Words: 674 |
Page: 1|
4 min read
Published: Oct 31, 2018
Words: 674|Page: 1|4 min read
Published: Oct 31, 2018
The pointer is one of the functions In C programming. The primary function of the pointer is act as an address referrer of a variable. The pointer obtains the value of a variable by referring its address. The relationship between pointer and c-programming is similar to index and book, the book contains everything but index can point out the item we are looking for through reference to the page number.
Pointer also acts as a simple and accurate tunnel for a program to allocate or refer the memory in a memory location. It acts similar to a personal secretary of a data or variable because it has the memory location and value of data. However, it also can be used as an indicator to display the value of the variable, while does not affect the variable itself. A sample program and output will be provided in the next section, further explanation will be provided.
To declare a pointer, ‘*’ or asterisk sign is used in pointer variable declaration.
An example of a single line declaration of a pointer variable is shown below.
Example 1:
int* a; //pointer declaration
Note: Since pointer variable is address based, the pointer variable type is integer.
Next, ‘&’ or ampersand sign is used to assign the address location of a variable;
Example 2:
int b=5;
int*a=&b; //pointer a is a reference to the address
The lines above are showing that the address of value of b, which is 5 has been assign to the pointer variable a. Now, the pointer a is capable to fetch the memory location and value of b to the user. However, to obtain the value of b, the dereference technique is used in the program.
To dereference a variable, which obtains the variable value of pointer holding, ‘*’ or asterisk sign is again used in the line. However, the context of using the asterisk sign in dereferencing is slightly different from the pointer declaration.
Example 3:
int b=5,c ; // we are using new variable, c to dereference pointer int*a=&b;
c=*a; // c is holding the value of b, which is 5
Now variable c is holding the value of b through using dereferencing method.
Example of program
#include
int pointer (int a) //function of pointer declaration
{ int*b = &a;
printf("The value of x is %dn",*b);
printf("The address of x is %dn",b);
printf("The address of pointer is %d",&b);
return 0; }
void main ()
{ int x =6; //main variable, which will be explained pointer(x); }
In this program consists of application of pointer which applying the address reference, dereferencing method and isolate the main variable from pointer, so that the main variable will not be affect by external environment.
The flow of the program goes like this:
Hence the output of the program shows like this:
Output:
The value of x is 6
The address of x is 6422256
The address of the pointer is 6422236
In conclusion, the application of pointer can act as a reference to point to the value of the main variable, which hold the memory location of the data simultaneously. The translation of the data has higher efficiency in the processor since it provides a guide or shortcut to point to the data.
Browse our vast selection of original essay samples, each expertly formatted and styled