Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pointer Problem

Posted on 1999-07-09
7
Medium Priority
?
219 Views
Last Modified: 2010-04-15
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
Comment
Question by:netminder
[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
7 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263452
cout <<
Looks mor like C++.

Anyway, what's the problem? So far it looks 'ok'
0
 
LVL 3

Expert Comment

by:Iexpert
ID: 1263453
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263454
Then you can just as well drop List as well... Q is equal to List in the Ex.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:netminder
ID: 1263455
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
 

Author Comment

by:netminder
ID: 1263456
To lexpert,

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


0
 

Accepted Solution

by:
apmontgo earned 300 total points
ID: 1263457
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
 

Author Comment

by:netminder
ID: 1263458
I first had 10, but after reading your comment I see my mistake.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

722 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