Tuesday, July 28, 2009

What is the significance of this statement in C/c++ ?

struct collision{


int j,time;


struct collision *parent,*next;


double cur_sep;


double delta_o;


double tandx, tandy, tandz;


};


--------------------------------------...


collision **collision_map=NULL; .....????


--------------------------------------...

What is the significance of this statement in C/c++ ?
Syntax error !!.





it should be





struct collision **collision_map = NULL;





this means that collision_map is a double pointer (pointer to pointer) pointing to the struct collision.





collision_map will contain the pointer value which is pointing to the structure defined above.





ie, if u want to access the members then u need to double driect the same. May be this way,





(* (*collision_map ) -%26gt;j)); will give u the vaue of int j,





regards


Decoy
Reply:This defines a structure in c/c++. The * denotes the variable adjacent to it is a pointer variable. The structure defined above looks as a doubly linked list node.





Here collision is the name of a strucuture. *parent and *next are pointers to other node.
Reply:In C:


It is a syntax error and it should be:





struct collision **collision_map = NULL;


Now, **collision_map is a double pointer, ie., it is a pointer to a pointer that can point to an instance of a structure. (But, not the structure itself).





For ex:


struct collision xObject;


struct collision *first;


first = %26amp;xObject;


struct collision *second;


second = %26amp;first;





Here 'second' is a double pointer. It is pointing to the pointer 'first' that in turns points to 'xObject'.





In C++


The syntax is correct.


Rest of the explanation held same and good.





* Note *


In the given statement, **collision_map is pointing to a NULL instead of a pointer to a structure instance. Hence, it is pointing to nowhere.


It can be used as an intialization statement.


This kind of structures are used to prepare double linked lists.
Reply:sorry i dont know


No comments:

Post a Comment