Thread: COnfuSing PoIntErS

  1. #1
    anamol12
    Guest

    COnfuSing PoIntErS

    Can some one help get started on an assignment involving pointers

    Create a class called Pointer that has only 1 data member, namely a char pointer. With this class, define 3 functions constuctor, destructor, and display function. The constructor accepts and saves 3 passed in variable of type char. This must be done dynamically by allocating memory for the 3 passed in variable. The display function must display, th the screen, the contents of the memory using pointer notation. Do this in reverse order in which you save the data. Implement the class using the proper c++ syntax..


    Proovide Driver function to invoke the display function

  2. #2
    anamol12
    Guest

    COnfuSed Still

    THIS IS WHAT I COME UP WITH SO FAR
    PLEASE HELP ME!!!

    class Pointer {
    private: char *p;

    public: Pointer(char , char , char );
    ~Pointer();
    Display();


    };

    Pointer::Pointer( char a, char b, char c)
    {
    p = new char;
    *p = a;
    }

    //Pointer::~Pointer();
    //{

    // delete p;
    //}

    Pointer:isplay()
    {
    cout << &p << endl;

    }

    main()
    {

    Pointer *P;
    P->Display();
    //P->~Display();




    return 0;

    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay there are a few errors. I'm glad you attempted it and asked for help instead of just asking someone to write it for you. Kudos to you.

    When you are allocating the memory in the constructor you need to allocate room for 3 characters instead of 1.

    The line should be:

    Code:
    p = new char[3];
    To indicate we want 3 characters. You may also want to check the pointer against NULL after this to make sure you actually got the memory. Then you need to set each character to it like this.

    Code:
    p[0] = a;
    p[1] = b;
    p[2] = c;
    Remember it is zero based so we don't index at 3. Basically we allocated using the [] to indicate we wanted an array of elements we need to change the delete line also to this:

    Code:
    delete [] p;
    The display code should look like this:

    Code:
    cout << p[2] << p[1] << p[0] << endl;
    Also don't call the destructor yourself! It will be called automatically at the end of the scope in which it was created in.

    Also you have a pointer to the Pointer Class in main but you don't allocate space for it. Either change it to a regular instance or allocate room. There is no problem doing:

    Code:
    Pointer p( 'a', 'b', 'c' );
    p.Display( );
    or...

    Code:
    Pointer *p = new Pointer( 'a', 'b', 'c' );
    p->Display( );
    delete p;
    I wrote this in a hurry so if any of it is confusing let me know.

    Any questions?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  3. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 08:30 AM