C Programming

String Pointer in C

A string is nothing but a character array that contains all character types of data type values. Through string, we can access any character with its index no. in a very fast manner. Today our topic is that we can access string through a pointer. It is called a string pointer. We will explain how the pointer will help us to access all the index of a string.

Accessing String Through Pointer

Char pointer:

  • String is stored in a char array.
  • Char s [10] =” Computer”,

  • Char pointer can point to char block.
  • Char *p; p=&s [0], p+1=1001, it represents the address of next block.

*(p+i) =s []

Explanation

In the above diagram, let us suppose we draw a pointer which is a character in type and declare a string whose base address is 1001. So the address of the next block is 1002. Because each character type value contains 1 byte in the memory, if the base address of the 1st block is 1001, so the next block’s address must be 1002 and so on. Now, pointer p holds the address of the 1st block means the base address of that particular string. It means the value of a pointer is 1001. Through this base address, we can access all the elements of the string easily.

String Constant

  • String Literal = string constant// string
  • Printf(“computer”); [which we write in “ “ that is called string constant or literal or string]
  • Char s [10] = “computer”;
  • Printf(s); only write s means it represents the address of the first block of the array s []. It means here we pass 1000as an address of the first block of array s [].
  • Strlen(s); we pass the address of the first block of array s [].
  • Strlen (&s [0]) = strlen(s) = strlen (“computer”);

Example-1

In this example, we can see through the pointer that we can calculate the total length of the string.

#include<stdio.h>

int length ( char *p )
{
    int count = 0 ;
    while ( *p != '\0' )
    {
       count++ ;
       p++ ;
    }  
return count ;
}

int main ()
{
    char str [ 100 ] ;  // declaring a string.
    int l ;
    printf( " \n Enter any string : " ) ;
    gets (str) ;
    l = length ( str ) ;    // length of the string.
    printf( " \n The length of the given string : %d \n ", l ) ;
    return 0 ;
}

Output

Explanation

Here we define a function names length (). In this function, we use a while loop where a condition is given that the loop will not be terminated until the pointer *p can access all the elements of the string. In the main () function, we declare a string named str[] to take a string from the user. Now we pass the string inside the parenthesis of the length () function to calculate the length of the string.

Example-2

Here we will see through the pointer we can reverse a string.

#include <stdio.h>

void reverse ( char [ ] , int , int ) ;

int main ()
{
    char Str [ 100 ] , temp ;   // declaring a string.
    int i , j , len ;
    printf( " \n Please Enter any String :  " ) ;
    gets ( Str ) ;
    len = strlen( Str ) ;
    reverse ( Str , 0 , len  - 1 ) ;    // reversing the string.
    printf( " \n String after Reversing = %s \n ", Str ) ;
    return 0 ;
}
void reverse ( char Str [ ] , int i , int len )
{
    char temp ;
    temp = Str [ i ] ;
    Str [ i ] = Str [ len - i ] ;
    Str [ len - i ] = temp ;
    if ( i == len/2 )
    {
        return ;
    }
    reverse ( Str , i + 1 , len ) ;
}

Output

Explanation

Here inside the main function (), we declare a string named str[] and take a string from the user with the help of the gets () function, except that we define a function named reverse () to reverse the string through a pointer which can access the values of str[].

Example-3

Here we will see through the pointer we can copy a string.

#include<stdio.h>

/* Function prototype */

void copy ( char s2 [ 30 ] , char s1 [ 30 ] ) ;

/* Main function */
int main ()
{
    char s1 [ 30 ] , s2 [ 30 ] ;
    int i ;

printf( " Enter string: \n " ) ;
    gets (s1) ;

    copy ( s2 , s1 ) ;

printf( " Copied string is: %s \n ", s2 ) ;

 return 0 ;
}

/* Function Definition*/
void copy ( char s2 [ 30 ] , char s1 [ 30 ] )
{
     int i ;
     for ( i = 0 ; s1[ i ] != '\0' ; i++ )
     {
        s2 [ i ] = s1 [ i ] ;
     }
    s2 [ i ] = '\0' ;
}

Output

Explanation

Here inside the main function (), we declare two strings named s1 [] and s2 [] and take a string from the user with the help of the gets () function in the string s1 []. Except that we define a function named copy () to copy the string of s1 [] to string s2 [] through a pointer that can access the values of string s1 [].

Example-4

Here we will see through the pointer that we can compare a string to another string.

#include <stdio.h>

// Function that compares the two string

void compareStrings ( char* x , char* y )
{
    int flag = 0 ;

    // iterate a loop till the end
    // of both the strings
    while (*x != '\0' || *y != '\0') {
        if (*x == *y) {
            x++ ;
            y++ ;
        }

        // If two characters are not same
        // print the difference and exit
        else if ((*x == '\0' && *y != '\0')
                 || (*x != '\0' && *y == '\0')
                 || *x != *y) {
            flag = 1 ;
printf( " Unequal Strings \n " ) ;
break ;
        }
    }

    // If two strings are exactly same
    if (flag == 0) {
printf( " Equal Strings \n " ) ;
    }
}

// Driver Code
int main ()
{
    // Given strings s1 and s2
    char s1 [ 20 ] = " python " ;
    char s2 [ 20 ] = " dsa " ;

// Function Call
compareStrings( s1 , s2 ) ;
    return 0 ;
}

Output

Explanation

Here inside the main function () we declare two strings named s1 [] and s2 []. In s1 [], we assign a value named “python” and in s2 [] named “dsa. “ Except that we define a function named compare () to compare the string of s1 [] and string of s2 [] through pointer which can access the values of string s1 [] and string s2 [] to compare both strings to each other. As two strings are different here, so the output of the string is an unequal string.

Conclusion

In this topic, we covered all the aspects of string pointer very seriously to understand the concept of the string pointer. It is very understandable that through a pointer, we can easily access the entire index of the string very fast and easy to make the code robust.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com