Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello World,

I was wondering if someone could explain STEP BY STEP how pointers work. I know what they are but I'm having trouble conceptualizing different situations. For example, What does each of these do?

1. pointer1 = pointer2->next;

2. pointer4 = pointer1;

3. pointer4->data = pointer1->data;

4. pointer4->next->data = pointer1->data;


I dont really understand what "->" does or what it means. I also don't quite understand the concept of "next". I would be so grateful if someone could break it down to the basics for me because I'm desperately trying to move ahead in my data structures class but really can't wrap my brain around this.


Thanks in advance,
Jennifer
Posted
Comments
Sergey Alexandrovich Kryukov 7-Apr-12 18:53pm    
There is not such concept as "next". It could be anything you can right. As you are a beginner, it could be a linked list, most likely. It's not good that you start a question from the middle, not showing all ins and outs.
--SA
jenbren 7-Apr-12 19:06pm    
That code above doesn't relate to a specific program - I just wanted to understand. What does it mean when you say 'p1 = p2->next". I just want a general answer like....This means p2 goes to the next and sets it equal to p1. I am just trying to understand the basics.
Sergey Alexandrovich Kryukov 8-Apr-12 0:51am    
It means that you first dereference pointer p1 into the object it points to and then take the value of the member "next", which can be anything. That's it.
--SA

This helped me when I was in school
http://www.youtube.com/watch?v=i49_SNt4yfk[^]
 
Share this answer
 
Also check out here. Simple and neat

http://cslibrary.stanford.edu/106/[^]
 
Share this answer
 
when you are using "->",it means you want to get the data in memory by the member name next to
"->" .And according to what you used above,the data structure might be defined like
C++
struct tagList
{
    int data;
    struct tagList *next;
};


and the pointers should be
C++
struct tagList *pointer1,*pointer2,*pointer3,*pointer4;


if pointer2 = &tmpList1;
then
pointer1 = pointer2->next;
it means pointer2 = tmp1.next;

if pointer4 = &tmpList2;
then
pointer4->data = pointer2->data;
it means tmpList2.data = tmpList1.data;
because pointer2->data is *(pointer2).data,and *(pointer2) is tmpList1;
hope it will helpful.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900