What is Pointer Variable in C | Detail Explained

Pointer Variable in C
Pointer Variable

In this article we will cover Pointer Variable in C in detail in which following points are included,

  • Pointer Variable
  • Declaration of Pointer Variable
  • Assigning address to Pointer Variable
  • Dereferencing Pointer Variable

Pointer Variable

A pointer is a variable that save memory address like any different variables it additionally has a name, has to be declared and occupies a few space in memory. It is called pointer because it factors to a specific place in memory by using storing the address of that location.

Declaration of Pointer Variable

Like different variables, pointer variables should additionally be declared earlier than being used The general syntax of assertion is-

data_type *pname

Here pname is the call of pointer variable, which need to be a legitimate C identifier the asterisk '*' preceding this call informs the compiler that the variable is said as a pointer. Here information type is called the bottom form of pointer. Let us take a few pointer declarations-

int *iptr;
glide *fptr;
char *cptr, chI, ch2;

Here iptr is a pointer th,at should factor to variables of kind int, in addition fptf and cptr need to point to variables of glide and char type respectively. Here sort of variable iptr is 'pointer to inC or (int *), or we will say that base type o( jptr is iQJ. We also can combine the announcement of simple variables and pointer variables as we've accomplished in the 0.33 announcement statement where chI and ch2 are declared as variables of type char.

Pointers also are variables so compiler will reserve space for them and they will also have a few cope(address) with. All pointers irrespective of their base kind will occupy same space in reminiscence(memory) in view that they all contain
addresses only. Generally 2 bytes are used to save an address with (may range in distinct computers), so the compiler allocates 2 bytes for a pointer variable.

Now the query arises that after all the recommendations include addresses simplest and each one occupies 2 bytes, then why we have to mention the data type in the declaration statement . We will come to know about this while we have a look at about indirection operator and pointer mathematics.

Assigning Address to Pointer Variable

When we declare a pointer variable it includes rubbish(garbage) value i.E, it could be pointing anywhere in the reminiscence(memory) so we should always assign an cope(address) with earlier than using it inside the program . The use of an unassigned pointer may deliver unpredictable results and even motive the program to crash. Pointers may be assigned the address of a variable the using assignment statement. Such as,

Int *iptr, age = 30;
float *fiptr, sal = 1500.50;
iptr = &age;
[ptr = &sal;

Now iptr incorporates the cope(address) with of variable age i.E. It point to variable age, similarly fptr points to variable sal. Since iptr is declared as a pointer of type, int, we should assign cope(address) with of handiest integer variables to it. If we assign Cope(address) with of some other information kind then compiler might not display any errors ,but the output will be incorrect.

Dereferencing Pointer Variable

Till now we've completed alot of programming and in all our packages we've got used the name of a variable for accessing We also can access a variable indirectly using pointer For this we will use the indirection operator( * ). By setting the indirection operator earlier than a pointer variable, we are able to access
the variable whose cope(address) is saved inside the pointer. Such as.

int a = 87
float b= 4.5;
int  *pI = &a;
float *p2 = &b;

Post a Comment (0)
Previous Post Next Post