Array Question.

This is a discussion on Array Question. within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> int main () { int x; char b[4] = {'a','b','ba','s'}; std::cout <<b[0] <<std::endl; std::cout <<b[1] <<std::endl; std::cout ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Array Question.

    Code:
    #include <iostream>
    
    int main ()
    {
    int x;
    char b[4] = {'a','b','ba','s'};
    
    std::cout <<b[0] <<std::endl;
    std::cout <<b[1] <<std::endl;
    std::cout <<b[2] <<std::endl;
    std::cout <<b[3] <<std::endl;
    std::cin >> x;
    return 0; 
    }
    Why does the array element b[2] only output one character? How do I make it to output both b and a instead of just a?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Element b[2] can only hold one char. It's the same as defining a regular, single char. It's set up as a space in memory that can only hold enough data for on character.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <iostream>
    #include <string>
    
    int main ()
    {
    int x;
    std::string b[4] = {"a","b","ba","s"};
    
    std::cout <<b[0] <<std::endl;
    std::cout <<b[1] <<std::endl;
    std::cout <<b[2] <<std::endl;
    std::cout <<b[3] <<std::endl;
    std::cin >> x;
    return 0; 
    }

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    to make it do what your wanting you would need to use a multi-dimensional array.

    Code:
    char TwoLetters[5][2] = { "ab", "cd", "ef", "fg", "hi" };
    I believe this is correct.

    EDIT: didnt notice your response swoopy.

    just think of my example as an alternate method

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >didnt notice your response swoopy.
    It's always a plus to have multiple methods at your disposal.

  6. #6
    1479
    Join Date
    Aug 2003
    Posts
    253
    What is the difference between the single quotes and the double quoates?
    Knowledge is power and I want it all

    -0RealityFusion0-

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Single quote are used for chars, and double quotes are used for strings and char arrays. A good way to remember this is

    Single quote - single char
    Multiple quotes - multiple chars

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    std::string b[4] = {"a","b","ba","s"};
    Why my vc++6.0 cannt support "string" type ,althought I have #include <string.h> ?

  9. #9
    1479
    Join Date
    Aug 2003
    Posts
    253
    Quote Originally Posted by toysoldier
    Why my vc++6.0 cannt support "string" type ,althought I have #include <string.h> ?

    Try #include <cstring> or #include <string>
    Knowledge is power and I want it all

    -0RealityFusion0-

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by RealityFusion
    Try #include <cstring> or #include <string>
    Must be

    Code:
    #include <string>
    since <cstring> is stuff from the C standard <string.h>, and doesn't include C++ string class.

    Dave

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ RealityFusion :
    Thanks , I will try it after go home.

    @Dave Evans :
    Thanks , I have try include <string.h> , maybe try include <string> again.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > char TwoLetters[5][2] = { "ab", "cd", "ef", "fg", "hi" };
    These are not 'C' strings, you do not allow room for the \0 at the end.
    Whilst you can do this in C, you're not even allowed to omit the \0 when compiling for C++

    If you want an array of string constants, try
    Code:
    char *words[] = {
      "hello",
      "world",
    };
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM

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