Solved

POINTERS AND ARROW

Posted on 2003-10-31
2
271 Views
Last Modified: 2010-04-02
please help me understand how pointers work and how this works "->" .  i really dont understand these concepts and i need step by step explanation of what they mean.  here are examples:


Struct Node { int data; Node *next;}; Node *p1, *p2, *p3;

p1->data = 112;//please explain this step
p2->data = 55;
*p1 = *p2; //please explain this step

cout << p1->data << "  " << p2->data<<endl;

the result of the cout is 55 55 but please help me step step explanation on how it works.
0
Comment
Question by:needle176
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Expert Comment

by:AmitAgarwal
ID: 9661768
pointer is actually a address where the data is stored.
so if you say p1 is a pointer to struct Node it means it is pointing to the address where Node is created.
-> is mechanism to access the members of structure or class using pointers.

so p1->data = 112;
means p1 is pointing to a node structure and that structure has a variable called data and you are assigning 112 to that variable.

*p1 = *p2; //please explain this step

p2 is pointing to another structure whose value you have set to 55.
so when you say
*p1 = *p2;
it copies the content of one structure into another
so now p1 Node will be initialized with the values in p2 node.
thats why you are getting both the values as 55

amit
0
 
LVL 9

Accepted Solution

by:
tinchos earned 20 total points
ID: 9661780
The concept of pointer is the following..................

you have an object (or struct, or built in type)...... stored in some place in memory...........

a pointer to an object is just the memory address where that object is stored..............

imagine that p is a pointer that points to an object (lets call it a)

then *p is the object that the pointer is pointing to (a itself)
and p->something is the same than to call a.something

this means, it's like to call some method or variable of the object to which the pointer is pointing to

Struct Node { int data; Node *next;}; Node *p1, *p2, *p3;        // In this line you declare 3 pointers (p1, p2, p3) to a struct

// Here you set that the value of the "data" member of the object pointed by p1 is equal to 112
p1->data = 112;//please explain this step                    
// Here you set that the value of the "data" member of the object pointed by p2 is equal to 55
p2->data = 55;

// Here you set that the value of the "data" member of the object pointed by p1 is equal to the value of the "data" member of the object pointed by p2 (this is 55)
*p1 = *p2; //please explain this step

// You print the 2 data members, that are both 55 (see the step before)
cout << p1->data << "  " << p2->data<<endl;

Hope it was clear enoughj

Tincho
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question