the concept of a string in c is difficult


  The concept of a string in C is difficult, because a string is a collection of characters stored in memory terminated by a NULL string i.e. \0. Let us consider the string Hello. This occupies 6 addresses in memory i.e.

    4000    H
    4001    e
    4002    l
    4003    l
    4004    o
    4005    \0
 
Therefore in order to declare a string in C we really declare a pointer to the string and C will stop reading in the string when it reaches the NULL terminator. Consider the program which prints your name 


    #include stdio.h
    void main()
    { 
    char prompt;
    /*Author : Mr James Mc Carren 
    Company: Staffordshire University 
     Date: 26th August 2012
    Version 1.0 
    Function : String storing illegally       
     Modifications:   none*/
    char *name;
    name = "James Mc carren";
    printf("your name is %s \n\r",name); 
    printf("Press and key to exit \n\r");
    scanf("\n%c",&prompt);
    }
 
Care should be taken with Loading up pointers as C needs to allocate space to any variable. The statement   name = "James Mc carren"; allocates 16 spaces. Consider the following program 
 
    #include
    void main()
    {
    char prompt;
    /*Author : Mr James Mc Carren 
    Company: Staffordshire University 
     Date: 26th August 2012 
    Version 1.0 
    Function : String storing illegally       
     Modifications:   none*/
    char *name;
    scanf("%s",name);
    printf("your name is %s \n\r",name); 
    printf("Press and key to exit \n\r");
    scanf("\n%c",&prompt);
    }

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: the concept of a string in c is difficult
Reference No:- TGS0414796

Expected delivery within 24 Hours