Pointers & Strings: A Simple Understanding

by

* If you’re here then probably you are beginner and trying to quickly understand the topic. So here everything is defined very simple in points so you can quickly understand everything.

Variables:

  • A variable is something that contains different values at different times.
  • In a Program, variable has;
    • Name
    • Type
    • Size
    • Value (For this we use assignment “=” Operator).
  • It creates a variable reserves appropriate space in memory.
  • Here,
    • An int is used to store an integer in reserved memory.
    • A char is used to store a character in its reserved memory.

For Example:

  • Char x;                 // reserves bytes in memory.
  • Int y;                    // reserves 2 byte in memory.
  • Float z;                // reserves 4 byte in memory.
  • Each memory location has two components contents and an address.
    • Contents are values stored on that memory location         &
    • Address is offset from starting memory location.
  • It is only the schematic representation. Actual contents of memory are a combination of binary 0’s & 1’s.
  • So according to figure;

In case of y binary equivalent of 10 will be stored

&

In case of x binary equivalent of ASCII code of ‘a’ will be stored.

Pointers:

* The Concept of Points is stick with comparatively low level languages such as C/C++.

  • Pointers are also the variables but on the replacement of any value, they store the address of some memory location.
    • Its means that;
      • The contents of a pointer variable are the address of some other memory location.
  • Declaration of Pointers:
    • Int * myptr;
  • Read as:
    • “ myptr is Pointer to an Integer “
  • Similarly;
    • int* pi;                    // creates a pointer to int
    • char* pc;               // creates a pointer to char
    • double * x…

Similar to simple variables memory location reserved for pointer will have two components

  • contents

&

  • Its address

Here, The contents of a pointer are the address of another memory location.

  • Unary Operator [ &  ]:
    • Use to returns the address of any variable

So, According to figure;

  • int *p;                // pointer p is created at address 150 (  where p has some garbage value )
  • int x=43;          // an int x with contents 43 is created at address 290.
  • p=&x ;              // & is a unary operator returns the address of x i.e. 290

                                    // now the contents of p is 290

                                   // i.e. p points to 290 and 290 is a pointer of p

  • cout<<x; // print value of x i.e. 43
  • cout<<p; // print value of p i.e. 290

Memory of Pointers:

The number of bytes a pointer takes in memory depends on the operating system environment;

  • In 16-bit environment it takes 16 bit or 2 bytes.
  • In 32 bit environment it takes 4 bytes.
  • In 64-bit environment it takes 8 bytes and so on.

All pointers are same as for as their contents are concerned the contents of every pointer is the address of some other memory location.

Like;

  • “int*” stores address of a memory location.
  • char* also stores address of a memory location.
  • long* also stores the address of a memory location.

The type of a pointer tells the compiler the type of contents of memory location pointed by pointer.

Like;     int* x; tells the compilers that the memory location pointed by x will have an integer value.

  • This information is useful in a variety of ways including indirection and pointer arithmetic’s.

Note: pointer cannot de-reference until assigned a value.


Indirection:

Indirection is used to access the value at memory location pointed by the pointer, also called dereferencing.

  • Indirection can be performed while accessing a variable, put “a * “ before variable name.

Example:

cout<<*p;                      // will print the contents of memory location pointed by p.

int *p;

int x=43;

p=&x ;                              //assigning address of x to pointer p

cout<<x;                         // print value of x i.e. 43

cout<<&x;                      // print address of x i.e. 290

cout<<p;                        // print value of p i.e. 290

cout<<&p;                     // print address of p itself i.e. 150

cout<<*p;                     // print value of memory location whose address is stored in p. i.e. 43

  • At indirection the type of value pointed by the pointer tells the compiler the number of bytes to deference. In case of int it will pick 2 bytes and in case of char it will pick 1 byte.
    • As the pointer just stores the starting address so it’s compulsory to tell compiler number of bytes to dereference.
  • Example:

int *pi;

int x = 43;

pi = &x ;

char * pc;

char y = ‘a’;

pc = &y

cout << *pi;                                     // 2 bytes will dereference ( i.e. 43) as pi is an int pointer

cout << *pc;                                     //only 1 bytes will dereference (i.e. a)as pc is a char pointer


Pointer Arithmetic:

  • Pointer arithmetic is a special type of arithmetic.
  • The pointer arithmetic are dependent on pointer type;

int* p;

p++;                               // adds 2-bytes to the pointer  address, since type is int.

long* pd;

pd++;                             // adds 4-bytes to the pointer  address, since type is long.

  • Similar case with other basic & user defined data types as the compiler knows how many bytes to go forward.
  • Following are the allowed pointer operations;
    • Subtraction of pointers results in the number of elements between the pointers. When two pointers subtract then it shows how many units the two pointer of their data type.
    • Addition of integer values in pointers
    • Add or subtract pointers with an integer value
    • “Adding two pointers are not allowed”
  • Example: (C++)

#include <iostream.h>

void main()

{

int* p1;          // simple way of defining pointers

int* p2;

int* p3;

int x=90;

p2 = &x;

p1 = p2;         // assign value of p2 into p1

p2 = p2 + 5; // adds 10 bytes in p1

cout << (p2 – p1);   // prints the number of

// elements between the //two pointers, which is 5 //p3 = p2 + p1; is illegal operation

}


Pointer Variable & Constant:

  • By Default, Pointers are variables.

Declaration:

int y[10];

int  * yptr;

yptr = y;

yptr++ ;                     // Now, memory address will be increment by depend upon its data type. Here it is increment by 4-bytes.

* yptr++;                   // Now, it will increment the value of yptr.

Just like;;

yptr = y                       

// is same as;

yptr = &y[0];

yptr = &y[2];

  • If we can write on the memory address then it will not happened.

Exammple:

Int x = 10;

Int * yptr;

yptr = &x;

* yptr += 3;                          //Here, the value of x increment by 3

ypt += 3;                               //Here, the address of yptr increment by “12 . (In 16-bit environment)

  • Similar with Decrement.
  • By using Pointer Arithmetic, we can judge the value of int, char etc…
  • Constant Pointers can be declared by using the “const” keyword. Array name is known as constant pointer.

Declaration:

int iVar = 10;

int* const iPtr = &iVar;                             // iPtr is a pointer, which is const, that points to an int .

  • In the above example you can change the value in iVar but not the address in iPtr.

Like;

*iPtr = 20;                // allowed because the pointer (i.e. the address) is constant, while the

                                   //value inside the pointer can be variable.

iPtr=100;                  // not allowed as iPtr (the pointer) is constant.

Data Scientist & Evangelist || IBM IBV Research Hub || IBM Recognised Speaker, Mentor and Teacher || Debater || Blogger || Guinness World Record Holder || Watson Application Developer || IBM Community Activist || Aspiring to Inspire.

Leave a Reply

Your email address will not be published.

*