1
$\begingroup$

Since any node points to a memory location is there any specific need in using it's pointer as structure type.Why couldn't we use integer as the pointer type in linked list rather than structure?

$\endgroup$
  • 2
    $\begingroup$ What is the context of your question? A specific course? A specific programming language? What nodes are you talking about? $\endgroup$ – babou Mar 23 '15 at 13:21
  • $\begingroup$ This seems to be a programming question to me. Please clarify how this is a computer science question, if it is. $\endgroup$ – Raphael Mar 23 '15 at 16:39
2
$\begingroup$

The nodes in a linked list need to hold two things:

  • the node's value
  • a (possibly null) pointer to the next node

That makes them structs, so a pointer to a node is a pointer to a struct.

$\endgroup$
1
$\begingroup$

You can use an untyped pointer if you wish, but the advantage of having a pointer to a structure is type safety – it helps you protect yourself from misusing the pointer. When implemented in machine code, the pointer is indeed untyped.

It's not a terribly good idea to implement a pointer as an integer, however, since the sizes of both are implementation dependent and could differ. On your system a particular type of integer has the same size as a pointer, but on another system that same type of integer has a different, perhaps smaller, size. Sometimes you even have to worry about several types of pointers.

$\endgroup$
  • $\begingroup$ :Do you mean to say that a structure pointer is implementation independent?I would like to know whether we could use an integer pointer instead of a structure pointer in linked list? $\endgroup$ – justin Mar 24 '15 at 11:23
  • $\begingroup$ This also depends on implementation, though I imagine it's always the same kind of pointer. But why would you want to use the "wrong" kind of pointer? It's not type safe and works against you as a programmer. $\endgroup$ – Yuval Filmus Mar 24 '15 at 12:03
  • $\begingroup$ :Just curious to know whether there is any specific difference between structure pointer and integer pointer other than the notion that "both points to a memory location". $\endgroup$ – justin Mar 24 '15 at 12:22
  • $\begingroup$ There could be. It could be implementation-dependent in a given programming language. You're not necessarily promised that they are the same. For example, it might happen that integers and structures are stored in different parts of memory. $\endgroup$ – Yuval Filmus Mar 24 '15 at 12:28
  • 1
    $\begingroup$ No, it just stores a memory address. But when you use it, the programming language assumes that it points to a structure. $\endgroup$ – Yuval Filmus Mar 24 '15 at 13:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.