Thread: A question on Pointers & Structs

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    Question A question on Pointers & Structs

    Im studying for my final on c++ and the instructor gave us some questions to study that will help us in the final exam. There is this one question that I didnt know how to solve. Here it is:
    ==========
    Declare a pointer variable "structPointer" and initialize it to point to a struct variable called "newPhone", of type "Phone", that has three int fields called "country", "area" and "number". Also write the declaration of the "Phone" type. Then write assignment statements to indirectly store the values 1, 888, 5551212 into these fields.
    ==========

    It seems simple and I tried to do it but sometimes I just get lost. Can someone please solve this for me. The final is tomorrow and Im probably going to need to know this.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I'm just guessing but...... something like this maybe?

    Code:
    struct Phone
    {
    	int country;
    	int area;
    	int number;
    };
    
    int main()
    {
    	Phone newPhone;
    	//...
    	Phone *structPointer = &newPhone;
    	structPointer->country = 1;
    	structPointer->area = 888;
    	structPointer->number = 5551212;
    	//...
    
    	return 0;
    }
    The question was kind of ambiguous, so this might not be what they meant.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    I find it kinda ironic that someone who has "do your own homework" in their sig gives out the answer like that!

    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    I was so close. The Phone type thing got me lost. The problem is we did things like this in class but I always forget the dumbest things.

    Thank You

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The way I figure it... his final is tomorrow. He's gonna fail regardless of what I do.

    Nah, just kidding. He just wants to see some work before he writes a test. That's fine with me, especially since the question was written so ambiguously, that the tax codes make more sense.

    As such, I have no idea if what I provided is even correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about pointers in C
    By Kilua in forum C Programming
    Replies: 3
    Last Post: 06-05-2009, 02:33 AM
  2. Pointers to pointers question
    By mikahell in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2006, 12:54 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM