Game programming

Moderators: None (Apply to moderate this forum)
Number of threads: 2070
Number of posts: 5361

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Edit Report
struct _point *next; ? help Posted by GuruMuhk on 28 May 1999 at 12:03 PM
Hi There..<p>
I am having a little problem here, Ill describe it<br>
with a bit of code, maybe you will figure out what I do wrong<br>
I mean I know what I do wrong, I just dont know how to do it<br>
the 'right way'.. here I go<p>
typedef struct _point {<br>
int x;<br>
int y;<br>
int z;<br>
struct _point *next;<br>
} point;<p>
ok, the problem is when I want to attach a point<br>
to another point.. what I do is,..<p>
void attach ( point *dest, point *src )<br>
{<br>
while (dest)<br>
dest = dest->next;<p>
dest = src;<br>
}<p>
it works well when dest == NULL;<br>
but when dest is already pointing to a point<br>
it will over write the existing point because the pointing <br>
address of dest will be over written by the next point<br>
anytime I do dest = dest->next;<p>
I could always do<p>
if (!dest->next)<br>
dest->next = src;<br>
else if (!dest->nest->nest)<br>
dest->next->next = src;<p>
but you know that is not good to do, I mean it I have 100<br>
points to attach, can you imagine the amount of<br>
->next->next->next->next I would have to do ?<p>
Please if somebody can tell how<br>
how I can go attach a point to a last ->next which is<br>
NULL without over writting my preview point ?<br>
You dont know how thankful I would be :)<br>
thanks :)


Edit Report
Re: struct _point *next; ? help Posted by Rock on 29 May 1999 at 5:59 PM
: typedef struct _point {<br>
: int x;<br>
: int y;<br>
: int z;<br>
: struct _point *next;<br>
: } point;<p>
: ok, the problem is when I want to attach a point<br>
: to another point.. what I do is,..<p>
: void attach ( point *dest, point *src )<br>
: {<br>
: while (dest)<br>
: dest = dest->next;<p>
: dest = src;<br>
: }<p>
I may be misunderstanding since I'm not sure how<br>
this would work even when dest = NULL. I see it<br>
would assign dest to src, but that is a local <br>
copy of dest. When the function returns, dest <br>
should still be NULL. (unless your not passing in<br>
a pointer but an address of a node).<p>
If you want your attach() function to append<br>
to the end of the list, and dest is presumably a<br>
pointer to the head of the list, then the <br>
easiest way is to do something like the following :<p>
point * attach(point * dest, point * src)<br>
{<br>
if(!dest)// empty list is special case<br>
dest = src;<br>
else<br>
{ // find last node in list<br>
while(dest->next)<br>
dest = dest->next;<br>
// append new node to end<br>
dest->next = src;<br>
}<p>
return(dest); // return the possibly new head<br>
}<br>
And invoke it something like:<p>
point * head = NULL;<p>
head = attach(head, src1);<br>
head = attach(head, src2);<br>
.....<p>
It isn't the most elegant, but it is the easiest.<br>
If I misunderstood, please clarify.<p>
Rock


Edit Report
Re: struct _point *next; ? help ( I found a way ) Posted by GuruMuhk on 31 May 1999 at 9:16 AM
Hi There.. thanks for the reply, I found a easy<br>
way to do it (maybe it is wrong), but it really<br>
seems to work ok without any problem so far<p>
I will copy my function here,..<p>
void vertex_attach_vertex ( Vertex *dest, Vertex *src )<br>
{<br>
Vertex *v;<p>
if ( (!dest) || (!src) )<br>
return;<p>
if (!dest->next)<br>
dest->next = src;<br>
else<br>
{<br>
v = dest;<br>
while (v->next)<br>
v = v->next;<p>
v->next = src;<br>
}<p>
}<p>
v may be a local vertex<br>
but v does point to the last vertex struct<br>
of dest<p>
I guess its the same thing as<p>
int i = 5;<p>
void dummy ( int *i )<br>
{<br>
i = 10;<br>
}<p>
dummy ( &i)<p>
i will == 10 ... right ?


Edit Report
Hey, where's my reply?!? Posted by Rock on 1 Jun 1999 at 5:04 AM
I posted it, and it shows up below your last post,<br>
but it isn't shown on the main board. What the heck?<p>
http://208.128.17.74/msgboard/board8/messages/683.html





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.