StudyBoss » Computer Programming » Pointer function in C programming

Pointer function in C programming

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:

  1. Function “pointer” is declared, the purpose of this function is used to declare the pointer variable.
  2. In the main function, the main variable x is declared, the value of 6 is assigned to it.
  3. Pointer function is called, the value of x is passed to it. The argument of function ‘a’.
  4. Pointer variable b is declared and assigned memory location from local variable a.
  5. There is three printf function, which points to the value of the main variable x, (dereference), the memory location of x, and address location of the pointer itself respectively.

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.

Cite This Work

To export a reference to this article please select a referencing style below:

Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.

Leave a Comment