Strings in C examples


Advertisements

Share



Learn strings declaration, indexing string elements, passing string to function, pointer to string, function returning pointer to string, arrary of strings, array of pointers to string with examples.

What are strings? Strings declaration

Frequently asked topics, concepts.
  • What are strings?
  • A character string is a series of one or more characters terminated by a null character. Here is an example of a string: "How are you doing!" The double quotation marks are not part of the string. They inform the compiler that they enclose a string, just like single quotation marks identify a character. Strings in C are always stored with a terminating null character. The presence of the null character means that the array must have at least one more cell than the number of characters to be stored.

  • String declarations?
  • 
    char cstr[] = {'A','B','C','D','E','\0'};
    char tstr[] = "ABCDE";
    char *pstr = "ABCDE";
    
    

    I have declared string in three ways. cstr is the way of declaring a string using characters. Generally you will not use this way while declaring strings in your project. But I have included this format here to highlight the fact that string has a null character as the last character.

    tstr is the string declaration using double quotes. This is the frequently used way of declaring string. pstr is the way of declaring string using pointer. This way of declaring string is also very frequently used.


    Accessing string elements and their addresses

    Frequently asked topics, concepts.
  • Accessing string elements
  • Like arrays string indexing starts from 0 not from 1.

    The address of a string is nothing but the name of the string. Also the addresses of string elements can be obtained using ampersand.

    Accessing string elements, addresses.
    
    #include "stdio.h"
    int main()
    {
      char cstr[] = {'A','B','C','D','E','\0'};
      printf("cstr addresses %x, %x and %x\n",cstr, &cstr[1], cstr+2);
      printf("cstr contents %c, %c and %c\n",cstr[0], cstr[1], cstr[2]);
      char tstr[] = "ABCDE";
      printf("tstr addresses %x, %x and %x\n",cstr, &cstr[1], cstr+2);
      printf("tstr contents %c, %c and %c\n",tstr[0], tstr[1], cstr[2]);  
      char *pstr = "ABCDE";
      printf("pstr addresses %x, %x and %x\n",pstr, &pstr[1], pstr+2);
      printf("pstr contents %c, %c and %c\n",pstr[0], pstr[1], pstr[2]);  
      return 0;
    }      
    
    
  • Program output:
  • cstr addresses 7e8f920c, 7e8f920d and 7e8f920e
    cstr contents A, B and C
    tstr addresses 7e8f920c, 7e8f920d and 7e8f920e
    tstr contents A, B and C
    pstr addresses 10660, 10661 and 10662
    pstr contents A, B and C
    

    In the program output notice that name cstr prints the address of string cstr as 7e8f920c. &cstr[1] prints the address of the second element of string cstr. Also notice that we can get address of the 3rd element using cstr+2. One more thing I want to highlight that as the a char takes 1 byte of space in memory the address keeps incrementing by 1. You will see similar observation for strings tstr and pstr.

    Passing string to a function

  • Illustration example
  • 
    #include "stdio.h"
    #include "string.h"
    #include "stdlib.h"
    void passStrToFunction(char *inStr,char *outStr);
    int main()
    {
      char *inStr = "Hello";
      char *outStr = malloc(strlen(inStr));
      passStrToFunction(inStr, outStr);
      printf("outStr:%s\n", outStr);
      return 0;
    }
    void passStrToFunction(char *inStr, char *outStr)
    {
      int i;
      for(i = 0;inStr[i] != '\0';i++)
      {
        outStr[i] = inStr[i];
      }
    }
    
    

    Output:

    $ gcc prog.c
    $ ./a.out
    outStr:Hello
    $
    

    Function returning pointer to string

    Frequently asked topics, concepts.
  • Illustration example
  • 
    #include "stdio.h"
    #include "string.h"
    #include "stdlib.h"
    char *retStrFromFunction(char *inStr);
    int main()
    {
      char *inStr = "Hello";
      char *outStr;
      outStr = retStrFromFunction(inStr);
      printf("outStr:%s\n", outStr);
      return 0;
    }
    char *retStrFromFunction(char *inStr)
    {
      char *outStr;
      outStr = malloc(strlen(inStr));
      int i;
      for(i = 0;inStr[i] != '\0';i++)
      {
        outStr[i] = inStr[i];
      }
      return outStr;
    }
    
    

    Output:

    $ gcc prog.c
    $ ./a.out
    outStr:Hello
    $
    

    Array of pointers to strings

    Frequently asked topics, concepts.
  • Illustration example
  • 
    #include <stdio.h>
    void ArrOfPtrsToStr(char *a[]);
    int main(void)
    {
      char *in[] = 
      {
        "Hello",
        "World"
      };
      ArrOfPtrsToStr(in);
    }
    void ArrOfPtrsToStr(char *a[])
    {
      printf("Got string:%s,%s\n",a[0],a[1]);
    }
    

    Exercises, Solutions

    Medium
    Medium
    High
    Low
    Low
    High
    High
    High
    Medium

    Want to contribute a new article? Write and upload your article information .
    Share

     Articles

     C Programming

     Linux Software

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude

     Embedded System Software

     Search Code Snippets

     Popular C search examples

     Popular C search MCQs