pointers basics

HELLO GUYS,HERE I WOULD LIKE TO DISCUSS ABOUT THE BASICS OF THE POINTERS IN DETAIL.IN ADVANCE I WILL DISCUSS LATER.

1)NOW,WHAT IS A POINTER ??

ANS: A POINTER IS NOTHING BUT IT IS A TOOL,WHICH KEEP TRACK OF THE SOMETHING OR IT EXACTLY SPECIFY THE LOCATION IS THE GENERAL DEFINITION OF THE POINTER . SAME IN THE CASE OF PROGRAMMING LANGUAGES .


2)WHAT IS THE POINTER AND HOW WE USE,IT ??

ANS : A POINTER IS VARIABLE(VARIABLE MEANS IT CHANGES TIME TO TIME, IN GENERAL ENGLISH). IN GENERAL,THE POINTER STORES THE ADDRESS OF ANOTHER
VARIABLE OR DYNAMICALLY ALLOCATED MEMORY.

IF SUPPOSE WE HAVE A POINTER THEN IT IS A TYPE INT POINTER,THEN IT POINTS TO THE ADDRESS OF THE VARIABLE OF THE TYPE INT.IN ABOVE, I DISCUSSED THAT POINTER A TOOL WHICH KEEPS TRACK OF VARIABLE, SO THAT WHICH SPECIFY THE LOCATION OF THE VARIABLE IN THE PROGRAM.


NOTE : POINTERS CAN BE INTERCHANGEABLE ONLY IN SOME CASES.
 
IN GENERAL, POINTERS ARE ALSO MOST DIFFICULT WHILE DEBUGGING THE PROGRAM IF ANY ERROR OCCURS.

THE POINTER IN THE PROGRAMMING LANGUAGE CAN BE DENOTED BY THE * .
*-----> ASTERISK SYMBOL, AND WHERE AS THE &---> AMPERSAND,IT IS USED TO STORE THE ADDRESS OF A VARIABLE.

EXAMPLE :
int *p , i;  // defining a pointer in c programming language.

HERE, *P --> DENOTES THE POINTER VARIABLE AND IT IS A INTEGER TYPE POINTER.

void(void *) is known as the null pointer and where as, the null pointer is defined in the standard header file  stddef.h in c programming language.

EX :
int *p=NULL; is the null pointer.

NOTE : a). Only * should be placed before the identifier not before the constant.
              b). when * placed before the identifier then we will get all the contents and address and        location of the variable that is identifier.

NOTE : a). for LP-32 bit programming , the size of the pointer or the memory occupied by the pointer is 4-bytes .
 
b). For ILP-64 bit programming, the size of the pointer is 8-bytes .

A VARIABLE OF A POINTER TYPE CAN ALSO BE INITIALIZED DURING DECLARATION

EXAMPLE :

>> int i
>> int *p = &i  // Initialization
>> i=10
>> *p
10

NOTE :
 IN THE ABOVE EXAMPLE , int *p=&i HERE, *p STORES THE VALUE OF i.

IN THIS TYPE OF DECLARATION 
EXAMPLE :

>> int i
>> int *p
>> p=&i
>> i =10
>> *p
10
IN ABOVE, WE OBSERVE THAT THE IF p=&i , THEN ALSO IT STORES THE VALUE OF 10.
THE TWO ABOVE DECLARATIONS ARE SIMILAR.

RELATIONAL OPERATORS WITH POINTERS

WE KNOW THAT RELATIONAL OPERATORS IN C (<,>,==,!=,>=,<=) THESE ARE RELATIONAL OPERATORS .

NOTE :
WE NEED TO REMEMBER THAT IF ONLY IF WE CAN COMPARE TWO POINTERS WHEN THEY BELONGS TO THE SAME DATATYPES .

POINTER ARITHMETIC

EXAMPLE :

int a[6] , *p;
p = &a[0];

then here, p stores the address of the a[0] that is the first element in an array.
 
WHEN EVER WE APPLY ARITHMETIC TO THESE POINTERS THEN IT CAN BE WRITTEN AS,

formulae :-    p+i = p + i*sizeof(*p)bytes
     that means we are adding the sizeof(*p)*i to the address stored in the p.


for suppose, let p=0x112356
    now let us consider that the *p belongs to integer data-type. Then, we know that size of pointer in 64-bit programming model is 8 bytes.Let i=1
now we have,

p+i=0x112356 + 1*8
    
  p+i=0x112364

BASIC OPERATIONS OF POINTERS :

>> int a[6]={100,200,300,400,500,600}, *p;
>> p = &a[0]   // here p stores the first-byte address of the a[0] that is first elemnt in the given array
>> *p
100  // output
>> p+=3   /*  let p=0x11235,then p=p+3 from above discussed example we know that p=0x11235+3*8
  p=0x11259 then here p points to the a[3]

>> *p
400 // output

>> *(p+1)
500             // p+1 points to a[4]
>>*(p-1)
300   // here p-1 points to a[2]

>> *p+1
401       // remember that now from p=0x11259 points to a[3] adding 1 to that element. i.e, 400+1=401.
                                                                            















































































































Protected by Copyscape

Comments

Popular posts from this blog

BIRTHDAY PARTY

spoj CPTTRN1 - Character Patterns (Act 1) solution

ROY AND PROFILE PICTURE