Could anyone pls explain to me how to create a dynamic two dimensional array of pointers. I searched the google, but I did not find anything useful that could help me solve the problem.
I tried to create the 2D array of pointers in the folowing way:
struct cluster_node
{
int vertex;
cluster_node *next;
};
cluster_node **cluster;
Afterwards I tried to allocate memory for rows and colomns:
cluster = NULL;
cg_edge_weight = new node * [20]; //allocate memory for rows
cluster[0] = new cluster_node[num_of_vertices+1]; //allocate memory for 0th row
Now if I try to assign the value NULL to cluster[0][0] (cluster[0][0] =NULL), I get a compiling error.
struct cluster_node {
// Best to provide a default constructor:
cluster_node() : vertex(), next() {}
// Make life easier (this is used to insert at front; may not be what you want)
cluster_node( int value, cluster_node* next ) :
vertex( value ), next( next ) {}
int vertex;
cluster_node* next;
};
cluster_node** hash_table = new cluster_node*[20]; // make hash table containing 20 rows
// null out the pointers
for( size_t i = 0; i < 20; ++i )
hash_table[ i ] = 0; /* NULL */
// Insert one element:
hash_table[ 4 ] = new cluster_node( 3, hash_table[ 4 ] ); // Inserts at front; may not be what you want
You are really helping a lot
. However, I'm not mixing arrays and linked lists. I just wanted to make an array of hash table-linked lists. Therefore, in this example, I wanted to allocate memory for 20 hash tables of linked list.
Ex
cluster_node cluster = new cluster_node * [20];
I hope that this time, I succeded to point where the problem is.