Array of srtings

This is a discussion on Array of srtings within the C++ Programming forums, part of the General Programming Boards category; I need some help. I was trying to create an array of strings. I used this code: #include <iostream> #include ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    87

    Unhappy Array of srtings

    I need some help. I was trying to create an array of strings. I used this code:

    #include <iostream>
    #include <fstream>

    using namespace std;

    int main()
    {
    char Line1[20] = "Hello Wazzup??";
    char Line2[20] = "Hello There!!";

    char * Index[2];

    Index[0] = &Line1[20];
    Index[1] = &Line2[20];

    cout << Index[0] << endl;
    cout << Index[1] << endl;

    return 0;
    }

    however when i compile Index[0] gives some strange ascii values and Index[1] contains &Line2... can someone tell me what i did wrong???

  2. #2
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    There's an easier way to do this, you could use a 2D array, but that's just a pain so an array of char *'s or an array of strings would be better.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
            // These strings can't be changed.
            char *arr_str[2] = {
                    "Hello Wazzup??",
                    "Hello There!!",
    
            };
    
            // These strings can be changed.
            string arr_str2[2] = {
                    "Hello Wazzup??",
                    "Hello There!!",
            };
    
            return 0;
    }
    The problem with your code was that you were giving the Index array the address of the 20th element of each array, this is out of bounds for both arrays so you get junk. This is your working code.
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
            char Line1[20] = "Hello Wazzup??";
            char Line2[20] = "Hello There!!";
            
            char * Index[2];
            
            Index[0] = Line1;
            Index[1] = Line2;
            
            cout << Index[0] << endl;
            cout << Index[1] << endl;
            
            return 0;
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Index[0] = Line1;
    Index[1] = Line2;


    should be

    strcpy(Index[0], Line1);
    strcpy(Index[0], Line2);

    as they are Cstyle strings, not STL strings. The assignment operator works on the latter, not the former. I know it was an oversight but mixing Cstyle strings with STL strings in the same post or the same program frequently leads to this type of error, much to my chagrin.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by elad
    Index[0] = Line1;
    Index[1] = Line2;


    should be

    strcpy(Index[0], Line1);
    strcpy(Index[0], Line2);

    as they are Cstyle strings, not STL strings. The assignment operator works on the latter, not the former. I know it was an oversight but mixing Cstyle strings with STL strings in the same post or the same program frequently leads to this type of error, much to my chagrin.
    Wrong! He's not copying the strings, he simply makes the pointers (in the array) point to the strings.

    Using strcpy like you suggest would try to copy the data to a random point in memory (the pointers in the array are undefined) and would most likely cause an access violation.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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