pointers

This is a discussion on pointers within the C++ Programming forums, part of the General Programming Boards category; here is my assignment: Write a program that declares a structure, dynamically creates an instance of the structure, and fills ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    85

    pointers

    here is my assignment:

    Write a program that declares a structure, dynamically creates an instance of the structure, and fills the structure with data.

    i know what a pointer is, but i don't really understand what the program is supposed to do. i mean, am i suppoed to get the data from the user and then use a pointer to store the data into the structure?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    "Write a program that declares a structure, dynamically creates an instance of the structure, and fills the structure with data. "

    It's simply an assignment to get you to understand the syntax of manipulating struct/class pointers.

    Creating dynamically means using the heap (new), rather than the stack.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    well, i haven't learned stacks or heaps yet so that doesn't help much.

    am i supposed to use the new and delete to allocate memory?

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    class whatever {
    int data;};

    int main() {
    whatever *ptrToWhatever = new whatever(); // Create a new whatever
    ptrToWhatever->data = 10; // Initilize member data
    delete ptrToWhatever; // Need to manually deallocate the memory because it was allocated with new
    return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    ok, i think i basically understand that, except i have one more question.
    ptrToWhatever->data = 10;
    why did you use ->, instead of ptrToWhatever.data = 10 ?

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ..because ptrToWhatever is a pointer. Normally you have to dereference the pointer to access that value stored where it is pointing -

    int a =10;
    int* ptr=&a;

    *ptr = 20; //dereference ptr to change value

    To do something similar with a class/struct object you'd have to do -

    (*ptrToWhatever).data = 10;

    because the . operator has higher priority than the * operator. As this doesn't look staightforward c/c++ offers the alternative syntax

    ptrToWhatever->data = 10;

    which is the one used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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