Array vs Pointer String: Difference You Should Know

Posted in /   /  

Array vs Pointer String: Difference You Should Know

Vinay Khatri
Last updated on December 11, 2022

    In programming languages such as C, C++, and Java, we use the pointer variable to store the address of some other variable. The pointer variable we create usually is of the same data type as that of the variable it points to. In C and C++, there is no built-in data type called string, so we basically use an array that 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 pointers and how the two are different from each other. But before discussing that, let’s have a brief overview of an array and a pointer.

    Array

    An array is a collection of elements of a common data type, and it stores all its elements sequentially in the memory. The array uses indexing (starting from 0 to n-1, where n is the total number of elements in the array) to access the stored elements. All the elements stored in the array have the same variable name but have a different index number.

    To access a specific element from an array, we use the index number associated with it. In both Java and C++, we have the concept of an array. However, there is a noticeable difference; in Java, arrays are dynamically allocated, whereas in C++, they are statically allocated.

    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 type that can hold 10 elements.

    Pointers

    A pointer is a special variable that is used to hold the memory address of another variable. The data type of the pointer variable depends on the data type of the variable whose addresses it is supposed to hold.

    If you want a pointer variable that 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.

    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 that is of integer type. * ptr is also an integer type because it is 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 an array because to make a string, we require the array data structure. The array has a property to bind all its elements in sequential order, and that’s why we use an array to hold a sequence of characters and call it a string.

    In C and C++ programming, apart from using an array data structure to access a string, we can create a string using pointers.

    Accessing a Sting Element

    • Sting as an Array
    • String as a Pointer

    String as an Array

    Conventionally, we use an array data structure to create a string. An array provides a simple way to represent a string so that all its characters are 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 be stored in the array memory as follows:

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

    To access the elements of string we can use indexing.

    String as a Pointer

    Using a pointer, we can access the string elements stored in the form of an array. As we know that the pointer should be of the same data type as that of the variable whose addresses it is supposed to hold, so for our example, the pointer would be of char type. A pointer can only hold one address at a time.

    So, initially, we hold the address of the first character of the string. Then, by using the arithmetic + operator, we increment the pointer address with the size of each character so the pointer can hold the address of the 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 the 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. An array is used to store the elements of the same data types.

    Holding Capacity

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

    Syntax

    Data type *variable_name Data Type  variable_name[size]

    Dependency (Data Type)

    The data type of a pointer variable depends on the data type of the variable whose address it stores. An array can hold only elements that have a common data type.

    Element Accessing Speed

    If we use a pointer to access the elements of a string, it would show better performance as compared 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 all its elements There could be an array of pointers.

    Storage

    A pointer stores the address of a variable. An array stores the value of variables.

    Conclusion

    A string can be accessed by 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.

    We hope that after reading this array vs pointer string article, you have developed a sound knowledge of what pointers and arrays are, and how they are different.

    People are also reading:

    FAQs


    An array is a data structure that stores data elements of a similar type. It stores all data items sequentially in memory.

    A pointer is a unique variable that stores the memory address of another variable.

    A string is a collection of sequential characters. To create a string, we use the array data structure as it sequentially holds the data items. So, a string is simply an array of characters in C.

    When you use a pointer to access the elements ot characters of a string, it provides better performance than array indexing.

    Leave a Comment on this Post

    0 Comments