Thread: Trouble Declaring

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    6

    Trouble Declaring

    Having trouble getting my head around declaring an array of C-strings(which themselves are arrays of chars). Any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like
    char strings[howmany][howlongeachoneis];

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Like:

    charArray1[]
    charArray2[]
    charArray3[]

    words[3]
    words[0] = charArray1
    words[1] = charArray2
    words[2] = charArray3

    (You have to fill in the types.)

    If you wanted to access the 2nd letter of charArray3, you would do this:

    charArray3[1];

    But since words[2] ==charArray3, you can make that direct substitution and write:

    words[2]
    [1]
    Last edited by 7stud; 02-16-2006 at 11:38 AM.

  4. #4
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    Or if you want to dynamically allocate the amount of memory you need for each string, use
    Code:
    char *array1[10];
    
    // allocate however much memory you need with new for each item

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    here's how i visualise what happens:

    declare char array [5] [10];

    here's what that looks like:

    Code:
        0  1  2  3  4  5  6  7  8  9
       -------------------------------
    0  |  |  |  |  |  |  |  |  |  |  |
       -------------------------------
       -------------------------------
    1  |  |  |  |  |  |  |  |  |  |  |
       -------------------------------
       -------------------------------
    2  |  |  |  | x|  |  |  |  |  |  |
       -------------------------------
       -------------------------------
    3  |  |  |  |  |  |  |  |  |  |  |
       -------------------------------
       -------------------------------
    4  |  |  |  |  |  |  |  |  |  |  |
       -------------------------------
    now cout<<array[2][3];
    will print out the value stored there, which is character x.
    think of that as a co-ordinate system - first comes y-axis, then
    x axis
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Any ideas?

    Switch to a vector of strings:
    Code:
    std::vector<std::string> myemptydata;
    // or maybe
    std::vector<std::string> tenstringsofdata(10);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Declaring instances of different implementations
    By dwks in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2008, 11:43 AM
  3. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  4. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM