Thread: Array Help Please

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    Question Array Help Please

    Goal:
    Write a program that compares the values stored in the first array to the user inputted values in the second array.


    In order to fix this error:


    Compiler Error C2078


    I had to change my array initialization to one with a star in front of it:


    Code:
    char a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};

    to:


    Code:
    char *a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};

    I also changed my 2nd array to one with a star in front of it:


    Code:
    char *a2[20];

    What does this mean exactly? Putting a star in front of an array?


    Also, I am now getting an "unhandled exception" when I try to get input for my 2nd array:


    Code:
    cin>>a2[i];

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The asterisk here means that instead of an array of char, you have an array of pointers to char. For a1, each pointer points to the first character of a null terminated string literal (which also means that it should have been const char* instead of just char*).

    For a2, you only have pointers, therefore you cannot read into a2[i] without allocating space.

    Why not use std::string, e.g.,
    Code:
    std::string a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};
    
    std::string a2[20];
    ?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    26
    Figured it out. Was using double quotes for char array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  2. Replies: 4
    Last Post: 05-30-2013, 05:45 PM
  3. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  4. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread