Array vs Pointer String

By | August 26, 2019

In programming languages such as C, C++ and Java we use pointer variable to point a value which address is stored in that pointer variable. The pointer variable we often create is of same data type, as the pointer variable is supposed to point.

In C and C++ there is no built-in data type called string, so we basically use an array which holds the characters in a sequence to form a string.

Here in this article, we have provided a simple explanation of how a string is related to arrays and pointer. Before that let’s have a brief introduction of what are Array and Pointers?

Array

An Array is a collection of similar data types elements which stored all its elements sequentially in-memory location. The array uses indexing (start from 0 to n-1, where n is the total number of elements stored in the array), to access the stored elements.

All the elements stored in the array shear the same variable name, with a different index number. To access specific element from an array we use the index number associated with it.

In both Java and C++, we have a concept of an array the difference is, in Java arrays are dynamically allocated and in C++ statically.

Syntax of Array:

Data_Type  Array_name[Size of the array];

Example:

int  x[10];

Behind the code:

In the above example, we defined an array x of integer types which can hold 10 elements.

Pointers

A pointer is a special variable we defined in the program which is used to hold the memory address of another variable. The Data Type of the pointer variable depends on the data type of that variable which addresses it is supposed to hold. If you want a pointer variable which holds the address of an integer then the pointer variable must be of integer type.

It is a special variable because when we define a pointer it contains an asterisk (*) symbol before the variable name. The asterisk (*) symbol before a variable makes it a pointer and guide it to point that value which addresses it hold.

Pointer syntax:

Data_Type *variable_name;

Example:

int a =10;
int *ptr;                                 //here ptr is a pointer variable name
ptr = &a;                              // here ptr collect the address of a 
cout<<*ptr;                        // Output will be 10;

Behind the code:

In the above example, we have defined a variable a of integer type

*ptr is also an integer type because it supposed to hold the address of the variable a

Variable *ptr is a pointer variable so ptr can hold the address of any integer type, using ptr = &a we passed the address of a in ptr variable.

String

A String is a collection of characters in a sequential manner. The definition of a string sound similar to Array, because to make a string we require the array data structure.

The array has a property to bind it’s all elements in a sequential order that’s why we use an array to hold a sequence of character and call it a string.

In C and C++ programming apart from using an Array data structure to access a string we can make a string using pointers.

Accessing of a Sting Elements:

  • Sting as an Array
  • String as a Pointer

String as an Array

By far we all know that conventionally we use an Array data Structure to create a String. Array provides a simple way to represent a String so that it’s all character stored in sequential order and the complete string can be accessed by a single variable name. 

Example:

void main()
{
char string[] = "Hello" ;
for(int i=0; string[i]!=’\0’;i++)
      cout<<string[i]<<endl;
}

Output:

H
e
l
l
o

Behind the Code:

The string Hello will store in the array memory as follow:

_____________
H|e|l|l|o|\0|
-------------

To access the elements of string we can use the indexing.

String as Pointers:

Using a pointer, we can also access the string elements stored in the form of Array. As we know that the pointer should be of same data type, which addresses it supposed to hold so the pointer would be of char type.

A pointer can only hold one address at a time so at first, we hold the address of the first character of string then using the arithmetic + operator then we increment the pointer address with the size of each character so the pointer can hold the address of next element and point to its value.

Example:

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main() {
             clrscr();
             char string[]="Hello",*ptr;
             ptr= string;
             while(*ptr!='\0')
                 {
                  cout<<*ptr<<endl;
                  ptr++;
                 }
  getch();
}

Output:

H
e
l
l
o

Behind the Code:

In the above example, we have used a pointer to access the elements of the string.

The variable string itself represents the address of the string[0] element and using the + operator we increment the address location by one character to get the address of next character.

String Pointer vs Array: Head to Head Comparison

Pointer

Array:

Explanation

A pointer variable is used to store the address of another variable The array is used to store the elements of the same data types.

Holding capacity

A pointer can hold the address on one element at a time An array can hold many elements at a time

Syntax

Data type *variable_name Data Type  variable_name[size]

Data Type Dependency

The data type of a pointer variable depends on the data type of address variable An array can hold only similar data types elements at a time.

Element Accessing speed:

If we use a pointer to access the elements of a string it would show better performance as compare to Array indexing Array indexing speed is less than Pointer accessing speed.

Generation

A pointer cannot hold an array though it can hold the base address of the array so we can access it’s all elements There could be an array of pointers

Storage

Pointer store the address of the variable An array stores the value of variables

Conclusion

A string can be accessed through using an Array or a pointer variable, using a pointer variable to access a string could be complex but it provides better performance than using an index.

People Also Read:

Leave a Reply

Your email address will not be published. Required fields are marked *