• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Pointer Problem

I have this code where List and Ptr are pointers of type S*, struct S has members Data of type int, and Next of type S*
List=new S;
Ptr=new S;
Ptr->Data=10;
Ptr->Next=List;
Q=Ptr->Next;
Q->Data=20;
cout<<List->Data;

Since this involves pointers(which are very confusing), could anybody help me make this work.

Thanks
0
netminder
Asked:
netminder
1 Solution
 
KangaRooCommented:
cout <<
Looks mor like C++.

Anyway, what's the problem? So far it looks 'ok'
0
 
IexpertCommented:
List=new S;
Ptr=new S;
Ptr->Data=10;
Ptr->Next=List;
Q=Ptr;                  //<--- This was error line, no Next needed
Q->Data=20;
cout<<List->Data;
0
 
KangaRooCommented:
Then you can just as well drop List as well... Q is equal to List in the Ex.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
netminderAuthor Commented:
To lexpert,
I definetly phrashed what I was looking for incorrectly. Actually I need the result(cout), this way I can check to see if I did code it correctly. Post what you get for an result. I will then award points to u. Sorry for the confusion.
0
 
netminderAuthor Commented:
To lexpert,

It would help if I opened the question up to answers. Same as other comment I gave before.


0
 
apmontgoCommented:
you neglected to mention what Q was but I assume that it is an unitialzed S pointer. What the code you gave does is create two S objects with List pointing to one(let's call it s1) and Ptr pointing to the other (let's call it s2). You then assign 10 to the data field in s2 through Ptr. you then have s2's next field point to s1 by using Ptr. You then bring in a new pointer called Q and assign it to Ptr->next which is ultimately s1. You then assign the data field of s1 = 20. When you print out List->data, who is still pointing at s1, you should get 20, as I did when I tested this code. Hope this helps.

0
 
netminderAuthor Commented:
I first had 10, but after reading your comment I see my mistake.
0

Featured Post

The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now