Thread: array of character pointers

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    23

    Unhappy array of character pointers

    Am new to c++, and am having some problems in initilizing arrays that hold strings during the program.
    I want to write a program that requests 5 names from the user
    and then stores it in an array of character pointers.
    Example:

    int main()
    {
    clrscr();
    char *names[5];
    int ctr;


    for(ctr=0; ctr<5; ctr++)
    {
    cout<<"\nWhat is the next name " ;
    cin>>*(names+ctr);

    }


    for(ctr=0; ctr<5; ctr++)
    {
    cout<<"Names:\n\n ";
    cout<<*(names+ctr) <<"\n";
    }

    return 0;
    }

    When a try to run this it just shows garbage . Can anyone give me a hint of where i went wrong.
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    7
    #include <iostream>
    #include <cstdlib>

    int main() {
    char *names[5];

    for (int ctr = 0; ctr < 5; ctr++) {
    cout << "Next Name: ";
    names[ctr] = (char *)malloc(32);
    cin >> names[ctr];
    }

    for (int ctr = 0; ctr < 5; ctr++)
    cout << "Names: " << names[ctr] << endl;

    free(*names);
    return 0;
    }

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    23
    Thanks for your help chris it works fine
    if it is possible to explain to me briefly this line


    QUOTE:
    names[ctr]=(char *) malloc (32);

    Why is it neseerary ? the malloc (32) i mean and what does 32 mean? if it was malloc (22) what would be the difference?

    Again Thanks for your help.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Re: array of character pointers

    PLEASE LOOK AT THE COMMENT THAT I MADE


    Originally posted by RedRum
    I want to write a program that requests 5 names from the user
    and then stores it in an array of character pointers.
    Example:

    Code:
    int main()
    {
         clrscr(); 
         char *names[5];
        int ctr;
        
        for(ctr=0; ctr<5; ctr++){
         cout<<"\nWhat is the next name " ;
         cin>>*(names+ctr); // YOU CAN'T DO THIS 
        }
    
    
        for(ctr=0; ctr<5; ctr++){
        cout<<"Names:\n\n ";
        cout<<*(names+ctr) <<"\n"; // YOU CAN'T DO THIS TOO
        }
    
          return 0;
    }
    When a try to run this it just shows garbage . Can anyone give me a hint of where i went wrong.
    As you are new in C++ ... try first to arrang your code...
    You just remind me in the way that I start...
    LQQK at the code that Chris wrote you .. and then try to solve it in another way.. this way you will learn more....
    C++
    The best

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    23
    Thanks for taking the time to look over my code and comment on it NANO. Am working on it now .

  6. #6
    Unregistered
    Guest
    BTW RedRum (char *)malloc(32) makes it so that the name inputted by the user can be up to 32 bytes. Making it malloc(22) would allocate 22 bytes.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Note also that the kosher C++ syntax for that is actually

    " names[ctr] = new char[32]; "
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  3. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Help with 2-D character array
    By choykawairicky in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 12:12 AM