what is the role of

Thread: what is the role of \0 place in an array..

place in an array..

This is a discussion on what is the role of

Thread: what is the role of \0 place in an array..

place in an array..
within the C Programming forums, part of the General Programming Boards category; why when we build an array like this: char st[]="abcd"; we have 5 cells in which the last cell is ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    what is the role of \0 place in an array..

    why when we build an array like this:

    char st[]="abcd";

    we have 5 cells in which the last cell is \0

    why is that

    and what are the number of each cell

    is it from 0-4

    or 1-5 ??

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Because at the lowest level, a string in C is just a sequence of characters. And since the length of the string isn't stored anywhere, you need a different way to tell where the end of the string is. So every string in C is terminated by a NULL character (\0), in most cases.

    In your example,
    Code:
    char st[]="abcd";
    you're right that st has five elements, and that the last one is \0. Since C starts counting from zero, these elements will be [0] through [4], and st[4] will be the \0.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so every array ends with \0 cell ??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    so every array ends with \0 cell ??
    No, every string ends with a '\0', but strings are arrays of characters.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Just an example: strlen calculates the length of a string. It does this by checking every character in the array for '\0' and counts upwards. When it finds '\0', it can return the length.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks i understand now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21