Results 1 to 5 of 5

Thread: Pointer arrays

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Pointer arrays

    Why can't I input into the pointer array?

    #include <iostream>
    #include <cstdlib>
    #include <cctype>

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

    for (int i=0; i<5; i++)
    cin>> g[i];

    for (int i=0; i<5; i++)
    cout<< g[i]<< endl;

    system("PAUSE");
    return 0;
    }
    Last edited by aewarnick; Jan 17th, 2003 at 06:38 PM.

  2. #2
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    Hi,
    Pointers and arrays always mess with me. Here is how I modified your program to work. I would give an explanation but I would probably describe it incorrectly. Some of the gurus on this site will do a much better job than I. :-)

    Anyhow, this code works....

    Code:
    #include <iostream> 
    #include <cstdlib> 
    #include <cctype> 
    
    int main(){ 
      char g[5];
      char  *p = g;  //this creates a pointer to g...I think 
      for (int i=0; i<5; i++) 
        cin >> p[i]; //this loads input into address
      for (int i=0; i<5; i++) 
        cout<< g[i]<< endl; 
      system("PAUSE"); 
      return 0; 
    }
    Regards,
    ChuckB
    I learn Robotics, Electronics and Game Programming at http://www.gameinstitute.com

  3. #3
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Pointer array is an array to store pointers.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yep, you're unlikely to receive a pointer as input from the user. And if that should be an array of strings: you don't allocate memory from them.

    For an array of strings you'd better write:
    Code:
    #include <string>
    using namespace std;
    
    //...
    string ar[5];
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Ok, thank you very much! All of you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width