close
test_template

Pointer Function in C Programming

Human-Written
download print

About this sample

About this sample

close
Human-Written

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:

  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

Get a custom paper now from our expert writers.

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.

Image of Alex Wood
This essay was reviewed by
Alex Wood

Cite this Essay

Pointer function in C programming. (2018, October 26). GradesFixer. Retrieved November 19, 2024, from https://gradesfixer.com/free-essay-examples/pointer-function-in-c-programming/
“Pointer function in C programming.” GradesFixer, 26 Oct. 2018, gradesfixer.com/free-essay-examples/pointer-function-in-c-programming/
Pointer function in C programming. [online]. Available at: <https://gradesfixer.com/free-essay-examples/pointer-function-in-c-programming/> [Accessed 19 Nov. 2024].
Pointer function in C programming [Internet]. GradesFixer. 2018 Oct 26 [cited 2024 Nov 19]. Available from: https://gradesfixer.com/free-essay-examples/pointer-function-in-c-programming/
copy
Keep in mind: This sample was shared by another student.
  • 450+ experts on 30 subjects ready to help
  • Custom essay delivered in as few as 3 hours
Write my essay

Still can’t find what you need?

Browse our vast selection of original essay samples, each expertly formatted and styled

close

Where do you want us to send this sample?

    By clicking “Continue”, you agree to our terms of service and privacy policy.

    close

    Be careful. This essay is not unique

    This essay was donated by a student and is likely to have been used and submitted before

    Download this Sample

    Free samples may contain mistakes and not unique parts

    close

    Sorry, we could not paraphrase this essay. Our professional writers can rewrite it and get you a unique paper.

    close

    Thanks!

    Please check your inbox.

    We can write you a custom essay that will follow your exact instructions and meet the deadlines. Let's fix your grades together!

    clock-banner-side

    Get Your
    Personalized Essay in 3 Hours or Less!

    exit-popup-close
    We can help you get a better grade and deliver your task on time!
    • Instructions Followed To The Letter
    • Deadlines Met At Every Stage
    • Unique And Plagiarism Free
    Order your paper now