No, it's not possible for an array of int
to contain itself.
There are some (likely non-portable) tricks you can play, like making one of the elements of the array be a converted pointer to the array:
int arr[10];
arr[5] = (int)arr;
but this doesn't make the array contain itself. The expression arr
, since it's of array type, is implicitly converted ("decays") to a pointer to its first element in most contexts, including this one. So, assuming the conversion doesn't lose any information, you can retrieve a pointer to the first element of arr
by converting arr[5]
back to type int*
. Note that this only gives you a pointer to arr
's first element; it loses any information about the length of arr
. And it's very common for an int*
pointer value not to fit into an int
without loss of information (on 64-bit systems, it's common for int*
to be 64 bits and int
to be 32 bits).
Integers, pointers, and arrays are three very different things. They are not simply interchangeable.
Recommend reading: section 6 of the comp.lang.c FAQ; it does a very good job of explaining the often confusing relationship between arrays and pointers in C.
Even in languages like Java and Ruby, an array can't actually contain itself. It can contain a reference to itself -- though the language might provide syntactic sugar that hides the fact that it's a reference. In C, such references are generally explicit.
What you can do is define a data structure that contains a pointer to an object of its own type. This is generally done with structures. For example:
struct tree_node {
int data;
struct tree_node *left;
struct tree_node *right;
};
This being C, you have to manage memory for your tree nodes explicitly, using malloc()
to allocate and free()
to deallocation -- or you can use an existing library that does that for you.