Initializing an array? what is goin on

This is a discussion on Initializing an array? what is goin on within the C++ Programming forums, part of the General Programming Boards category; I have done far more work in C then C++, but I decide to take a look at C++. Wen ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    111

    Initializing an array? what is goin on

    I have done far more work in C then C++, but I decide to take a look at C++. Wen you create an array say:

    char b[5];

    What do you use to initialize it?

    If i do:

    char b[5] = 'a','b','c','d','e'; //as it would be done in C

    I get an invalid initializer error.
    If I do it like this:

    char b[5] = "abcde";

    it says it is to many characters.

    How stupid am I being?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    If i do:

    char b[5] = 'a','b','c','d','e'; //as it would be done in C

    I get an invalid initializer error.
    That's not how you do it in C. In C/C++ you do this:

    Code:
    char b[5] = { 'a', 'b', 'c', 'd', 'e' };
    If I do it like this:

    char b[5] = "abcde";

    it says it is to many characters.
    Yes, for strings such as that, you are forgetting the null character at the end. That would require 6 characters, not 5:

    Code:
    char b[6] = "abcde";
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Oh, forgot the brackets. Thanks for the help, a null character, hmmm.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    C style strings use a character array to store the string data, but they add a null character to the end in order to indicate the size of the string (otherwise you would always have to have a separate variable that remembered the string length).

    If you intend your character array to be used as a string (e.g. to write to cout or use functions like strcmp), then you need the null character. If you are just using an array of five characters and you aren't using any functionality specific to strings, you are fine with the original 5 character array.

    Of course, if you were trying to work with strings, you wouldn't want to use C style strings anyway, you would use the C++ standard string class.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > char b[5] = "abcde";
    C allows you to silently drop the \0 from a string containing exactly the right number of characters. C++ does not.

    It's usual to just leave the brackets empty if you just want to initialise the array with as many characters as there are in the string.
    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.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    C++ arrays are similar to JavaScript arrays, except you define the array's variables as you use them.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Is the C++ string have any efficiency differences then the C-style string?

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    I didn't see any.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Well, if you consider the fact that you don't have to check bounds all of the time to prevent segmentation faults. Erasing characters is also much more efficient.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    alright, and one more question (I think), can you access an individual character from a string like you can with an array?

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    I seriously doubt that.

    First you have to define string: #include<string>

    ex:

    string mystring = "poopiepants";

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by mburt
    I seriously doubt that.

    First you have to define string: #include<string>

    ex:

    string mystring = "poopiepants";
    How does that make it less efficient, exactly?

    Quote Originally Posted by lilrayray
    alright, and one more question (I think), can you access an individual character from a string like you can with an array?
    Yes, you can. The string object has an overloaded subscript operator that works exactly like an array
    Code:
    std::string foo = "Hello World!";
    
    for(int i = 0; i < foo.size(); i++)
       std::cout << foo[i];
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    Got it. The only downside is that you have to store the binary number in an array.

    See this:

    Code:
    #include <iostream>
    using namespace std;
    int main() {
    int number[] = {1,1,1,1,0,0,0,0};
    int count = 0;
    for (int i=0;i<=8;i++) {
    if (number[i]==1) {count++;};
    };
    cout << count;
    system("PAUSE");
    }
    Last edited by mburt; 08-23-2006 at 08:33 PM.

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Hmm strings seam nice. I almost feel like i am betraying C.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Got it. The only downside is that you have to store the binary number in an array.
    Sounds like you intended to reply to counting 1's in a binary byte. If so, the bitset idea suggested by twomers is the way to go, considering that it has a count member function.
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Initializing a 2D Array in C
    By Cell in forum C Programming
    Replies: 20
    Last Post: 03-21-2009, 12:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 04:19 PM

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