Briefly Explain about Pointers? How to Access the Address of a Variable? How to Declare, Initialise & Accessing a Pointer Variable?




The computer’s memory is a sequential  collection of storage cells. Each cell, commonly known as a byte, has a no called add.associated with it. Every byte has a unique address number.

Typically the addes are numbered consecutively starting from 0. The last add depends on the memory size.

when ever we declare a variable the system allocates, somewhere in the memory, an appropriate location to hold the value of the variable.

Ex:– int qty= 179;

Represetation of a variable.
 pointers


We may have access to the value 179 by using either the name qty or the address 5000.

Since memory addresses  are simply numbers they can be assigned to some variables, that can be stored in memory, like any other variable.

Such variables that hold memory addresses are called pointer variables.

A pointer variable is , there four nothing but a variable that contains an address, which is a location of another variable in memory. If we assign the address of qty to a variable.

pointers1

Since the value of variable P is the address of variable qty, We may access the value of qty by using of p and, there four we say that the variable P points to the variable qty.

Accessing the add of the variables:

The actual location of a variable in the memory is system dependent. The operator & immediately preceding a variable returns the address of the variable associated with it.

Ex:– P=&qty;

Would assign the add 5000 to the variable P. The & operator can be remembered as ‘address of'.The & operator can be used only with a simple variable or an array  etc.

Ex:– 
main()
{
int x;char a;
a=’A’;x=125;
printf(“%c is stored at address %u\n”,a,&a);
printf(“%d is stored at address%u\n”,x,&x);
}

Declaring pointer variables:–

In c, every variable must be declared for its type. Since pointer variables contain addresss that belong to a separate data type, they must be declared as pointers before we use them.

Syntax:– datatype * pointername;

This tells the complier 3 things about the variable pointer name.

1. The  asterisk(*) tells that the variable pointername is a pointer variable.

2. pointer name needs a memory location.

3. pointer name points to a variable of type datatype.

Ex:–  int *P;

Declares the variable P as a pointer variable that points to a integer data;

Ex:– float *x;

Declares x as a pointers to a floating point variable.

Initialization of Pointers variables:–

The process of assigning the address of a variable to a pointer variable is known as initialization.

Once a pointer variable has been declared we can use the assignment operator to initialize the variable.

Ex:– int qty;

 int *P; //declaration

P=&qty; //initialization.

We can also combine the initialization with the declaration. i.,e,

int *p=&qty;  is allowed.

The only requirement here is that the variable qty must be declared before the initialization.

We must ensure that the pointer variables always point to the corresponding type of data.

If is also possible to combine the declaration of data variable, the declaration of pointer variable, & the initialization of the pointer variable in one step.

Ex:– int x, *P=&x;

Accessing a variable through its pointer:–

Once a pointer has been assigned the add; of a variable, we can access the value of the variable using a array operator * (asterisk), usually known as the indirection operator.

Another name for the in director operator is the dereferencing iperator .

Ex–: 
 int qty, *p, n;
qty=179;
P=&qty;
n=*p;

The first line declares qty & n as integers variables & P as a pointing to an integer.

The second line assigns the value 179 to qty & the 3rd line assigns the address of qty to the pointer variable P. 

The 4th line contain the indirection operator * is placed before a pointer variable in an expression.

The pointer returns the value of the variable of which the pointer value is the address.

In the case * P returns the value of the variable qty, because P is the address of qty.

The * can be remembered as value at address.

Ex:–  two statements P = & qty;     n=*p;    are equal to 
                                      n=*&qty; (or) n=qty;

Ex:–
main()
{
int x, y;
int *ptr;
x=10; ptr=&x;
y=*ptr;
printf(“the value of x is %d”,x);
printf(%d is stored of add %u \n”, x, &x);
printf(%d is stored of add %u \n”, *&x, &x);
printf(%d is stored of add %u \n”, *ptr,, ptr);
printf(%d is stored of add %u \n”, y, &y);
*ptr=25;
printf(“now x=%d\n”,x);
}

Out Put:
 
value of x is 10
10 is stored at add 4104.
10 is stored at add 4104.
10 is stored at add 4106.
4/04 is stored at add 4106
10 is stored at add 4108
Now x = 25

Related

C Language 3899173521185381517

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item