Summarising and tweaking, it should look like this:
Node.h
#ifndef NODE_H
#define NODE_H
typedef void * DataP;
struct Node
{
DataP data;
void * key;
}
#endif
Node.c
#include <stdlib.h> /* for malloc () and free() */
#include <errno.h> /* for errno */
#include "node.h"
void nodes_free(struct Node ** ppNode, size_t rows)
{
if (!ppNode)
{
return;
}
for (size_t row = 0; row < rows; ++row)
{
free(ppNode[row]);
}
free(ppNode);
}
int nodes_allocate(struct Node *** pppNode, size_t rows, size_t columns)
{
if (!pppNode && !*pppNode)
{
errno = EINVAL;
return -1;
}
*pppNode = malloc(rows * sizeof **pppNode);
if (!*pppNode)
{
perror("malloc() failed on allocating rows");
return -1;
}
{
size_t row = 0
for (; row < rows; --row)
{
(*pppNode)[row] = malloc(columns * sizeof *(*pppNode)[row]);
if (!*pppNode[row])
{
perror("malloc() failed on allocating columns");
break;
}
}
if (row < rows) /* Allocation of columns did not completed successfully,
clean up and leave. */
{
nodes_free(*pppNode, row);
return -1;
}
}
return 0;
}
Use those functions like this:
#include <stdlib.h> /* for EXIT_xxx macros */
#include <stdio.h> /* for perror() */
#include "node.h"
#define ROWS (5)
#define COLUMNS (7)
int main(void)
{
struct Node ** ppNode;
if (-1 == nodes_allocate(&ppNodes, ROWS, COLUMNS);
{
perror("nodes_allocate() failed");
return EXIT_FAILURE;
}
/* Do stuff. */
/* clean up. */
nodes_free(*ppNodes, ROWS);
return EXIT_SUCCESS;
}