The pointer in C is a very important part of programming. The pointer’s primary function is to refer an address of a varible. By referring to a variable’s address, the pointer can obtain its value. The relationship that exists between pointer, c programming and index is similar to that which exists between book and index. Book contains all information but index can indicate the item to be found by referring to a page number.

The pointer can also be used as a quick and accurate way for a programme to refer or allocate memory at a specific location. The data is stored in the memory, and it acts as a personal assistant. But it can also be used as a value indicator for the variable. The next section will include a sample of a program and its output. Further explanations will also be given.

Pointer variables are declared using the * or asterisk.

The following is an example of how to declare a pointer in one line.

Instance 1:

int* a; //pointer declaration

Note: Because pointer variables are address-based, they have an integer type.

The address of a given variable is assigned by using the ‘&” or ampersand symbol;

Sample 2:

Int b=5

The address is indicated by the pointer int*a=&b

The lines below show that a variable called a has been given the address and value of 5 for b. The user can retrieve the memory address and value of the b using the pointer variable a. In order to determine the value for b, dereference techniques are used.

The asterisk symbol or the ‘*’ sign are used to dereference variables, which gives the value of the variable that holds the pointer. The context in which the asterisk is used to denote a variable differs slightly from that of the declaration.

Instance 3:

The pointer is dereferenced by using the new variable c.

c=*a ; // C holds the value b of 5

Dereferencing is used to assign the value b to variable c.

Program Example

#include

int pointer(int a). // Function of the pointer declaration

Printf( “The value for x %dn”,*b);

Printf(“The address for x is %dn”,b);

Printf(“The pointer’s address is %d”,&b);

The result is zero.|Return 0;|return 0|Return 0|return 0}

void main ()

An integer variable, x, is assigned a value of 6. This will be used in the pointer function.|int (x) = 6; //main variable which will be explained by pointer(x);|int 6 =; //main variables, which are explained with pointer (x);|int =6; //main varible, which is explained later pointer(x);|x = 6; //main parameter, which will explained in pointer(x);}

This program includes the dereferencing of address, the application of a pointer and the isolation of main variable.

The program flows as follows:

Declaring the function “pointer” declares the variable pointer.

The variable x declared in the function main is given the value 6.

The value x of the pointer is passed on to the function. The argument to function ‘a.

Local variable a is used to declare the pointer variable and assign it a memory location.

There are three different printf functions that point to the value x of the variable (dereference), x’s memory location, and the address for the pointer.

The output shows as follows:

Output:

The value for x = 6

The address is 6422256

The address is 6422236

As a conclusion, pointers can be used as a guide to indicate the value of’main variables, which simultaneously hold the location in the memory of data. It is more efficient for the processor to translate the data since it acts as a shortcut or guide to point at the data.

Author

  • evelynnrobertson

    Evelynn Robertson is a 27-year-old blogger and volunteer. She is also a student. Evelynn is originally from the United States but is currently living in the United Kingdom. She is a graduate of the University of Alabama. Evelynn is passionate about education and is always looking for new ways to help others learn. She is also a big fan of travel and enjoys exploring new places.