Pointers in C language

Pointers in C language

The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.

Consider the following example to define a pointer which stores the address of an integer.int n = 10;   int* p = &n;

pointer in c language AndroWep-Tutorials
pointer in c language AndroWep-Tutorials
int n = 15;   
int* p = &n;//pointer type variable

Declaring a pointer

The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.

int *n = 15; //pointer type variable  
char *p = &n;//pointer type variable

Example

// Program to illustrate pointers
#include <stdio.h>
int main (void)
{
int   count = 10, x;
int   *int_pointer;
int_pointer = &count;
x = *int_pointer;
printf ("count = %i, x = %i\n", count, x);
return 0;
}

Output

count = 10, x = 10

The variables count and x are declared to be integer variables in the normal fashion.On the next line,the variable int_pointer is declared to be of type “pointer to int.” Note that the two lines of declarations could have been combined into the single line
int count = 10, x, *int_pointer;

Next,the address operator is applied to the variable count.This has the effect of creating a pointer to this variable,which is then assigned by the program to the variable int_pointer. Execution of the next statement in the program,
x = *int_pointer;

proceeds as follows:The indirection operator tells the C system to treat the variable int_pointer as containing a pointer to another data item.This pointer is then used to access the desired data item,whose type is specified by the declaration of the pointer variable.

Because you told the compiler that int_pointer points to integers when you declared the variable,the compiler knows that the value referenced by the expression *int_pointer is an integer.And because you set int_pointer to point to the integer variable count in the previous program statement,it is the value of count that is indirectly accessed by this expression. You should realize that Program 11.1 is a manufactured example of the use of pointers and does not show a practical use for them in a program.Such motivation is presented shortly,after you have become familiar with the basic ways in which pointers can be defined and manipulated in a program.

*int_pointer is an integer.And because you set int_pointer to point to the integer variable count in the previous program statement,it is the value of count that is indirectly accessed by this expression. You should realize that Program 11.1 is a manufactured example of the use of pointers and does not show a practical use for them in a program.Such motivation is presented shortly,after you have become familiar with the basic ways in which pointers can be defined and manipulated in a program.

Pointer Example

// Further examples of pointers
#include <stdio.h>
int main (void)
{
char  c = 'Q';
char  *char_pointer = &c;
printf ("%c %c\n", c, *char_pointer);
c = '/';
printf ("%c %c\n", c, *char_pointer);
*char_pointer = '(';
printf ("%c %c\n", c, *char_pointer);
return 0;
}

Output

Q Q
/ /
( (

Using Pointers in Expressions

Two integer pointers, p1 and p2,are defined.Notice how the value referenced by a pointer can be used in an arithmetic expression.If p1 is defined to be of type “pointer to integer,”what conclusion do you think can be made about the use of *p1 in an expression?

// More on pointers 
#include <stdio.h>
int main (void)
{
int  i1, i2;
int  *p1, *p2;
i1 = 5;
p1 = &i1;
i2 = *p1 / 2 + 10;
p2 = p1;
printf ("i1 = %i, i2 = %i, *p1 = %i, *p2 = %i\n", i1, i2, *p1, *p2);
return 0;
}

Output

i1 = 5, i2 = 12, *p1 = 5, *p2 = 5

What are C pointers used for?

Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc.

How a pointer is used?

pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.