Thread: Array of character strings

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    12

    Array of character strings

    Hi,
    I am a complete newbie, and have got a problem in using an array of character pointers.
    When I run this piece of code:

    char *record[3];
    cout<<"Enter your name:";
    cin.getline(*record,10);
    cout<<*record;

    Windows gives me an error in my program’s .exe file.
    What’s wrong?

    Thanks in Advance
    Nima

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you have forgotten to allocate any memory. There are several ways of doing it....

    1) a 2d array..... char [3][255];

    2) use new to allocate memory. remember to use delete to free it.

    3) use malloc() to allocate memory. remember to use free() to free it.

    For your code a 2d array would be easiest.

    secondly you might want to read up on exactly how to use the dereference operator * because you have a few errors in syntax.

    char * MyString="A string";

    Mystring is the pointer.
    *Mystring is the object pointed to. In this case "A string"
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-16-2008, 12:15 AM
  2. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 05:48 AM
  3. watching character array in visual studio
    By neandrake in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2006, 11:12 PM
  4. Array of Strings
    By mjpars in forum C Programming
    Replies: 8
    Last Post: 08-21-2003, 11:15 PM
  5. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 04:53 AM