Array of character strings

This is a discussion on Array of character strings within the C++ Programming forums, part of the General Programming Boards category; Hi, I am a complete newbie, and have got a problem in using an array of character pointers. When I ...

  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, 06:48 AM
  3. watching character array in visual studio
    By neandrake in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2006, 12:12 AM
  4. Array of Strings
    By mjpars in forum C Programming
    Replies: 8
    Last Post: 08-22-2003, 12:15 AM
  5. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 04:53 AM

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