Like Tree2Likes
  • 1 Post By Ancient Dragon
  • 1 Post By Ancient Dragon

an array of names (strings)

This is a discussion on an array of names (strings) within the C++ Programming forums, part of the General Programming Boards category; how would i declare an array of namers? i would like to hold 10 names in one variable. how do ...

  1. #1
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55

    an array of names (strings)

    how would i declare an array of namers? i would like to hold 10 names in one variable. how do i do that??

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    The easiest way is to declare an array of strings like so:
    Code:
    string names[10]={"Bob","Joe","Belzebub",etc etc};

  3. #3
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    but i would like to use 'char' insted of 'string' ...sorry i should've been alittle more specific in my first post.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    I'd suggest a vector:
    Code:
    vector<string> names(10);
    Since you are using C Style strings then you would use const char* instead of string in PJYelton's example, but you wouldn't be able to modify the string values.

    If you want to be able to modify the values, you could use a two dimensional array of char, or a one dimensional array of char* and use a loop with new to initialize each pointer with the character array.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    This creates an array of an unspecified number of strings. The compiler will figure out how many strings by the number of strings in the initilizer list. If you want the array to have room for more later, then you will have to put a number between the brackets.
    Code:
    const char* names[]={"Bob","Joe","Belzebub",etc etc};
    But if you are writing a c++ program, then normally use std::vector class. Occasionally C-style string arrays are easier, especially when they don't change, such as an array of month names, or weekday names.
    Last edited by Ancient Dragon; 09-28-2005 at 03:40 PM.
    Alsaadawi likes this.

  6. #6
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    thx dragon, your my hero.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Ancient Dragon's strings can't be modified.
    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.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    Ancient Dragon's strings can't be modified.
    Of course not -- that's what the const keyword is for at the beginning of the declaration. If you want a modifiable array it need to be something on the order of this (an array that will hold 10 strings and each string can be up to 254 characters + null terminator). change the numbers however you wish.

    Code:
    char array[10][255];
    there are several other ways to do this too, but the above is the simplest.
    Alsaadawi likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 11:28 AM
  4. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  5. how to make an array of strings
    By rozner in forum C Programming
    Replies: 2
    Last Post: 03-24-2003, 12:18 PM

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