Strings Are One-Dimensional Arrays of Type Char

Strings Are One-Dimensional Arrays of Type Char

<p> Strings •Strings are one-dimensional arrays of type char •A string in C is terminated by the end-of-string sentinel \0, or null character •Like with all arrays, it is the job of the programmer to make sure that string boundaries are not overrun.</p><p>Example I: char *p =”abc”; printf(“%s %s\n”, p, p+1) ; /* abc bc is printed*/</p><p> “abc” is a character array of size 4 with the last element being null character \0</p><p> The variable p is assigned the base address of the character array “abc”. </p><p> When a pointer to char is printed in the format of a string, each successive characters in the array are printed until the end-of string sentinel is reached.</p><p>Example II: char * p=”abcd”; and char s[ ]=”abcd”; </p><p>In the first declaration the compiler allocates space in memory for p, puts the sting constant “abcd” in memory, and inilializes p with the base address of the string. Basically p is pointing to the string.  In the second declaration, the compiler allocates 5 bytes of memory for the array s. The first byte is initialized with ‘a’, the second with ‘b’ …and fifth one with a null character \0 .</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us