Thread: C++ Array of Strings

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22

    C++ Array of Strings

    I have a question, a newbie question, though:

    I need to create an array of strings like this:

    Code:
    groupBits7 = ['10','11','12','13','14','15','16','17','18','19','1A','1B','1C','1D','1E','1F']
    That is python, I need to make something similar in C++, and that allows me to access it like array[2].

    Thanks.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    char groupBits7[] = { '10','11','12','13','14','15','16','17','18','19','1A','1B','1C','1D','1E','1F' };
    Careful that you do not access out of bounds.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    Code:
    char groupBits7[] = { '10','11','12','13','14','15','16','17','18','19','1A','1B','1C','1D','1E','1F' };
    Careful that you do not access out of bounds.
    That should be:
    Code:
    const char *groupBits7[] = { "item1", "item2", "item3" };

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Thinking about it... of course, that was my mistake. I would do it this way:
    Code:
    std::string groupBits7[] = { "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F" };
    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.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    @Elysia
    You surprised me. An array?

    How about:
    Code:
    std::vector<std::string> groupBits7 = { "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F" };
    Jim

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you want to go that route, then:
    Code:
    std::array<std::string> groupBits7 = { "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F" };
    However, this requires initialization lists which is only available in GCC.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Location
    Portugal
    Posts
    22
    Quote Originally Posted by Elysia View Post
    Thinking about it... of course, that was my mistake. I would do it this way:
    Code:
    std::string groupBits7[] = { "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F" };
    This is what I like the most

    I have another question, how to use more than one function? Ok, I'm not that dumb:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
      cout << "Hello World";
      cout << sum2Numbers(10,200); //Will return 210
      cin.get();
      return 0;
    }
    int sum2Numbers(int a, int b) {
      return a+b;
    }
    My question is, how do I declare the second function? I know I have to do that, but can't remember why and how.

    EDIT Never mind, I got around it.
    Last edited by ScoutDavid; 03-26-2011 at 12:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM