char array of words

This is a discussion on char array of words within the C++ Programming forums, part of the General Programming Boards category; Hi,I wanted to have a array for char words. That is have a list of words like one two three ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    24

    char array of words

    Hi,I wanted to have a array for char words. That is have a list of words like one two three

    So, tried
    Code:
    char *num[]={"One","Two","three"};
    And it works fine. But later I wanted to assign the char words manually.So i tried the following,but it fails to work.

    Code:
       char *num[5][10];
      
       strcpy("one",num[0]);
      strcpy("two",num[1]);
      strcpy("three",num[2]);
    dunnno if its coding error or wrong methodolgy(if wrong do tell me correct one.thanx).

    sujeet1

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    You've got the order of the arguments to strcpy wrong. Also, num shouldn't be a 2-D array of pointers in this case:
    Code:
    char num[5][10];
      
    strcpy(num[0],"one");
    strcpy(num[1],"two");
    strcpy(num[2],"three");
    Since this is the C++ board, maybe you should try an array or vector of strings:
    Code:
    vector<string> strVect;
    
    strVect.push_back("one");
    strVect.push_back("two");
    strVect.push_back("three");
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    although vector is much convenient that array, arrays offer much more functionality and push you to your limits.

    aren't chars contained in single quotation marks ??
    'char' ??
    , note: I did not compile this code, it's just for demonstration
    Code:
    char* charArray[5];
    for(int i = 0; i < 5; i++)
    charArray[i] = new char;
    
    *charArray[0] = 'H';
    *charArray[1] = 'E';
    *charArray[2] = 'L';
    *charArray[3] = 'L';
    *charArray[4] = 'O';
    
    //Deleting 
    for(int i = 0; i < 5; i++)
    delete[] charArray[i];
    charArray[i] = 0;
    Last edited by Hussain Hani; 05-24-2007 at 10:30 PM.
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Hussain Hani View Post
    although vector is much convenient that array, arrays offer much less functionality and require you to write much more code to achieve the same thing, but probably slower and with more bugs
    corrected

    How on earth do arrays offer more functionality than vectors?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,451
    delete[] is for new[]

    you have new char - it sould be deleted with regular delete
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > although vector is much convenient that array, arrays offer much more functionality and push you to your limits.
    Besides the fact that vectors let you do anything that arrays do (often more easily and just as efficiently - assuming you use reserve() and/or resize() to avoid unnecessary reallocation when you know in advance an upper bound for the size), you don't WANT to be pushed to your limits by an unnecessarily limited programming language (otherwise, why not use assembly?), but by choosing harder problems to code. Features like vectors let your limits go farther.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM

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