Linked List Problem
I created one linked List and one of the elements of the linked list is int. After that I assigned String value to that element. It compiled successfully and it is working.
Code in C is
struct node {
int data;
struct node* next;
}
........
..........
struct node* head = null;
head = malloc(...)
head->data = "Hello";
I can also print the elements using printf("%s", head->data);
Is it possible ?
regards,
namanc
[518 byte] By [
namanc] at [2007-9-30 15:19:41]

> Is it possible ?Yes, in C that is possible; especially when you ignore your compiler diagnostics and assume thatsizeof(int) == sizeof(char*). In Java we all use a pre-cooked LinkedList for those purposes though.kind regards,Jos
JosAH at 2007-7-5 22:24:09 >

You may be able to get quite close to this 'casting', if you do like this:
class node {
public Object data;
public node next;
}
node pNode = new node();
pNode.data = new Integer(5);
pNode.data = "This is a string";
System.out.println( (String) pNode.data );
The great advantage vs C++ is that if pNode.data is not a String, it will throw an ClassCastException instead of printing rubbish or crash.