Just to make sure I'm thinking right.

This is a discussion on Just to make sure I'm thinking right. within the C Programming forums, part of the General Programming Boards category; Code: char arr[4] = { "a", "b", "c", "d" }; this array is being sorted to: a = arr[0], b ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    242

    Just to make sure I'm thinking right.

    Code:
    char arr[4] = { "a", "b", "c", "d" };
    this array is being sorted to:
    a = arr[0],
    b = arr[1],
    c = arr[2],
    d = arr[3],
    and \0 = arr[4].

    I know I'm wrong, but I just don't understand where \0 goes to.
    Last edited by eXeCuTeR; 11-26-2007 at 04:44 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is wrong.

    You have an array of char that is 4 long. Into it, you are trying to put pointers to the strings "a", "b", "c" and "d", each of which contain one letter and one NUL character.

    If you meant to use single quotes, you still got it wrong, as the array only has four entries. There is no arr[4] - it is the "fifth element of a four element array", and as such is definitely in the "undefined" section of programming - what it "contains" is very much depending on what the surrounding data is, and it could contain literally any value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Well if you'd written something which will compile, say this
    Code:
    char arr[4] = { 'a', 'b', 'c', 'd' };
    then there would be no \0 at the end of the string

    Neither does this have a \0
    Code:
    char arr[4] = "abcd";
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    So when there's a \0?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When you have a text string and there's enough space in the array.

    E.g.
    Code:
    char arr[5] = "ABCD";
    char arr[] = "ABCD";
    char arr[] = { 'A', 'B', 'C', 'D' };
    char *str = "ABCD";
    Just some variatins on the theme.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    If I have something like this, what will happen? 2 \0s?
    e.g:
    Code:
    char arr[6] = "ABCD";

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, there will be "enough zero's to fill the array", because ALL initialized data is "filled with zeros for anything that you haven't specifically defined".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Great, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 08:51 PM
  2. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  3. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  4. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM

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