I have a struct like this;
struct abc
{
char bbb[10];
char ccc[4];
char ddd[6];
int i1;
int i2;
struct abc *prior, *next;
};
struct abb *start, *last, *this, *temp;
I have a lot of code using these pointers start,last, etc. so I would like to use them but I would like to add pointers to the struct and pointers within the struct to accomplish the following:
load the struct up with data then depending on the value of i1 for example, display and change data when the value of i1 = 0 or when the value of i1 = 1 or the contents of the struct regardless of the value of i1. And, at the end of the day save the whole struct to file with changes made in any of the three conditions.
I thought to have added pointers such as these:
struct abc *prior1, *next1;
struct abc *prior2, *next2;
};
struct abb *start1, *last1...etc.
struct abb *start2, *last2...etc.
I can have:
start = start1;
last = last1;
But how then do I reference
prior1
next1
Or tell me a better way of doing this.
It sounds like you're asking for advice on a serialization strategy. I'd suggest writing free operator<< and operator>> methods for handling I/O on your struct, where the pointers are referenced in a map that assigns an ascending numeric ID value to each. When writing, upon visiting each struct pointer, you check the map, and if the ID is found, you simply write out the ID and nothing else. Otherwise, you add the next available ID to the map, assign the ID to the pointer, and then write out the entire struct. When reading, you reverse the process, reading in each ID and looking in the map for that pointer. If it's not found, you have the first instance, so you proceed to read in the entire struct and store it in the map. Otherwise, you retrieve the previously read struct from the map and assign the pointer. This general strategy is used by CArchive serialization (Microsoft Foundation Classes). There may be some open source libraries to do the same.
Your question is not very clear.
You might just need to check out C pointers, and such syntax as start->prior which references the value of prior contained within start.
E.g.
// Set the i1 value of start to 42
start->i1 = 42;
printf("The answer is: %d\n", start->i2);
Or you might want to have the equivalent of methods to interact with one of your structures.
For example:
struct abc
{
char bbb[10];
char ccc[4];
char ddd[6];
int i1;
int i2;
struct abc *prior, *next;
};
struct abb *start, *last, *this, *temp;
/**
* Set the BBB of struct target to a given value.
* #param value the value to set
* #return pointer to the target
*/
struct abc *setBBB(struct abc *target, char *value)
{
strncpy(target->bbb, value, sizeof(target->bbb));
return target;
}
char *getBBB(struct abc *target)
{
return target->bbb;
}
struct abc *next(struct abc *target)
{
return target->next;
}
int getI1(struct abc *target)
{
return target->i1;
}
to accomplish the following: load the struct up with data then
depending on the value of i1 for example, display and change data when
the value of i1 = 0 or when the value of i1 = 1 or the contents of the
struct regardless of the value of i1.
This you would do with:
switch(getI1(start))
{
case 0:
setBBB(start, "Hello"); // NOTE: to treat this as a string,
// at most 9 characters are allowed,
// else the terminating zero will be lost.
break;
case 1:
setBBB(start, "World"); // ditto.
break;
...
}
And, at the end of the day save the whole struct to file with changes made in any of the three conditions.
You want to save the structure to a file, you need something like
int saveStruct(char *filename, int position, struct abc *target)
{
int ret;
FILE *fp;
if (NULL == (fp = fopen(filename, "r+")))
{
if (NULL == (fp = fopen(filename, "w")))
{
return -1; // Error.
}
}
fseek(fp, position * sizeof(struct abc), SEEK_SET);
ret = fwrite(target, sizeof(struct abc), 1, fp);
fclose(fp);
return ret;
}
The above has some problems, though. The values of pointers (*prior and *next) will become meaningless once saved to disk, and will remain so when loaded from disk. So the loadStruct() function will require as additional parameters the values of *prior and *next to be restored.
Also, you can't (but it depends on the platform) save a structure at position #1 if you haven't saved one at position #0 before. And each save performs an expensive open-and-close.
int loadStruct(char *filename, int position, struct abc *target)
{
int ret;
FILE *fp;
if (NULL == (fp = fopen(filename, "r")))
{
return -1; // Error.
}
fseek(fp, position * sizeof(struct abc), SEEK_SET);
ret = fread(target, sizeof(struct abc), 1, fp);
target->prior = NULL;
target->next = NULL;
fclose(fp);
return ret;
}
// The above, tested with the test case below, returns 'Hello!' as expected.
int main()
{
struct abc *start, *another;
start = malloc(sizeof(struct abc));
setBBB(start, "Hello!");
saveStruct("file.bin", 0, start);
another = malloc(sizeof(struct abc));
loadStruct("file.bin", 0, another);
printf("Re-read: %s.\n", getBBB(another));
}
Related
I have to implement a contructor function in c based upon this struct:
struct Node {
char name[MAX_NAME_LEN + 1];
NodeType type;
union {
struct {
Entry* entries; // list of directory entries
} dir;
struct {
void* contents; // any binary data of the given length
int length;
} file;
};
};
Now I want to build a constructor function to file, point to the contents and save the length in bytes but somehow I don't know how to do it...
Here is my Attempt:
i KNOW that i have to allocate space for this but how do i make this pointer stuff?
Node* new_file(char* name) {
Node* ptr_file = xmalloc(sizeof(Node));
ptr_file->name;
return NULL;
}
You need to typedef struct { ... } Node for your code to compile.
When using anonymous structs/unions you simply don't give a name for the anonymous member. ptr_file->dir.entries or ptr_file->file.contents.
Should you ditch the internal struct names and make those anonymous as well you would only need to type ptr_file->entries;.
Please note that anonymous structs/unions were added to the C language in the ISO 9899:2011 version of the language, so you need to use a fairly modern compiler to use them.
As a side note, the void* probably doesn't make any sense. What I think that you are trying to do is this:
#include <stdint.h>
typedef struct {
char name[MAX_NAME_LEN + 1];
NodeType type;
union {
struct {
Entry* entries; // list of directory entries
};
struct {
uint8_t contents [sizeof(Entry)]; // use to inspect raw data
int length; // probably not needed
};
};
} Node;
With Node* new_file(char* name), Node is not yet defined. Code needs typedef struct Node Node; or the like.
A big challenge to this task is the many potential errors that could creep in: file name too long, memory allocation failure, fopen open failure, read failure, file too long, ...
int length; should be size_t length; as files may be longer than INT_MAX, yet fit in an allocation.
new_file(char* name) looks like it should read a file. Let's make a helper function as there are various degrees of robustness needed. The below commonly "works" yet is technical UB (seeking to the end of a binary file). Adjust as needed.
Illustrative, untested code:
// The below does _not_ return a null character appended string,
// just the data in the file.
void *alloc_file(const char *file_name, size_t *size) {
*size = 0;
FILE *binary_stream = fopen(file_name, "rb");
if (binary_stream == NULL) {
return NULL;
}
long offset = fseek(binary_stream, SEEK_END);
rewind(binary_stream);
if (offset == -1 || (unsigned long) offset > SIZE_MAX) {
fclose(binary_stream); // Trouble or file too long
return NULL;
}
size_t file_size = (size_t) offset;
void *buf = malloc(file_size) {
fclose(binary_stream);
return NULL;
}
if (fread(buf, 1, file_size, binary_stream) != file_szie) {
fclose(binary_stream);
free(buf);
return NULL;
}
*size = file_size;
fclose(binary_stream);
return buf;
}
Now new_file is easier.
// Better as: Node* new_file(const char* name)
Node* new_file(char* name) {
if (strlen(name) >= MAX_NAME_LEN) {
return NULL // name too long
}
// Allocate to the size of the referenced object, not type.
Node* ptr_file = malloc(sizeof ptr_file[0]);
if (ptr_file == NULL) {
return NULL;
}
strcpy(ptr_file->name, name);
ptr_file->type = NodeType_file; // Some constant (OP has not shown yet).
size_t size = 0;
ptr_file->file.contents = alloc_file(name, &size);
if (ptr_file->file.contents == NULL || size > INT_MAX) {
free(ptr_file->file.contents);
free(ptr_file);
return NULL;
}
ptr_file->length = (int) size;
return ptr_file;
}
I'm trying to read/write linked list into a binary file, the problem is that this list contains
dynamically allocated strings and when I try to read from the file the linked list, I get in the string field, address instead of the string value.
Someone knows maybe what is the problem?
void saveProject(FrameNode* headFrame, char* filePath)
{
FILE* projectFile = fopen(filePath, WRITING_MODE);
FrameNode* currentFrame = headFrame;
if (projectFile) // making sure the fopen() didn't failed
{
while (currentFrame != NULL) // making sure the list isn't empty
{
fseek(projectFile, 0, SEEK_END); // writing the node to the end of the file
// writing the currentNode into the file and returning the value of ->"next"
fwrite(currentFrame->frame, sizeof(Frame), 1, projectFile);
currentFrame = currentFrame->next; // moving to the next frame
}
fseek(projectFile, 0, SEEK_SET); // returning the seek of the file to the start
fclose(projectFile);
}
}
void openProject(FrameNode** headFrame, char* filePath)
{
FILE* projectFile = fopen(filePath, READING_MODE);
FrameNode* currentFrame = *headFrame;
int numOfFrames = 0;
int i = 0;
if (projectFile) // making sure the fopen() function didn't failed
{
// making sure the headFrame doesn't point to existing list
dealloc_linked_list(*headFrame);
*headFrame = NULL;
// finding the number of nodes (=frames) in the projectFile file
fseek(projectFile, 0, SEEK_END);
numOfFrames = (int)(ftell(projectFile) / sizeof(Frame));
fseek(projectFile, 0, SEEK_SET);
for (i = 0; i < numOfFrames; i++)
{
// reading the next frame in the list
fseek(projectFile, sizeof(Frame) * i, SEEK_SET);
addFrameFromFile(headFrame, projectFile);
}
}
fclose(projectFile);
}
void addFrameFromFile(FrameNode** headFrame, FILE* projectFile)
{
FrameNode* newFrame = NULL;
if (*headFrame == NULL) // in case the list is empty
{
*headFrame = (FrameNode*)malloc(sizeof(FrameNode));
newFrame = *headFrame;
}
else // if the list isn't empty, the function will search for the last node in the list
{
newFrame = findLastFrame(*headFrame);
newFrame->next = (FrameNode*)malloc(sizeof(FrameNode));
newFrame = newFrame->next;
}
// adding the data from the file to the newFrame
newFrame->frame = (Frame*)malloc(sizeof(Frame));
fread(newFrame->frame, sizeof(Frame), 1, projectFile);
newFrame->next = NULL; // making the frame be the last in the list
}
And This is the linked list node structs:
// the content
typedef struct Frame
{
char* name;
unsigned int duration;
char* path;
} Frame;
// Link (node) struct
typedef struct FrameNode
{
Frame* frame;
struct FrameNode* next;
} FrameNode;
When I try to read the file, I get the next output:
click me to see the screenshot of the output
The first printing is the original list that I wrote into the file, and the second is the list I created from the file using the openProject() function.
Thanks in advance
Regarding;
typedef struct Frame
{
char* name;
unsigned int duration;
char* path;
} Frame;
and
fwrite(currentFrame->frame, sizeof(Frame), 1, projectFile);
the call to fwrite() is outputting the contents of an instance of Frame However, that does NOT output the data to be found where the pointers point.
Also, after writing out those pointers, then trying read them back in, the pointers are now meaningless.
Suggest modify the typedef ... Frame to actually contain the data rather than pointers to the data.
Suggest:
typedef struct Frame
{
char name[ MAX_NAME_LEN ];
unsigned int duration;
char path[ MAX_PATH_LEN ];
} Frame;
The will also eliminate the calls to malloc() and family, except for obtaining an instance of Frame
The fwrite() function is used to write the binary values from the active RAM in your program to a file, so when you call fwrite(currentFrame->frame,sizeof(Frame),1,projectFile) you're asking the system to copy sizeof(Frame) (times one) bytes from the memory pointed to by currentFrame->frame into your file.
In your setup, a Frame has two char* in it, but fwrite does not care about this. The fwrite() function will just treat these pointers like any other binary value, and copy the memory addresses, which will soon be incorrect, into the file.
If you want to save the important parts of your linked list into a file so you can reproduce a linked list with the same data in the future you need to actually copy the contents of the strings into the file, like so:
void saveProject(FrameNode* headFrame, char* filePath)
{
FILE* projectFile = fopen(filePath, WRITING_MODE);
FrameNode* currentFrame = headFrame;
if (projectFile == NULL) // making sure the fopen() didn't failed
return;
while (currentFrame != NULL) // making sure the list isn't empty
{
fprintf(projectFile,"%s%d%s",currentFrame->frame->name,currentFrame->frame->duration,currentFrame->frame->path);
currentFrame = currentFrame->next; // moving to the next frame
}
fclose(projectFile);
}
fprintf() works just like printf, except it writes to a file instead of stdout, so it will write the strings into the file not the pointers that point to them.
Note that all the fseeks in your code are unnecessary as the file cursor is automatically moved as you write to the file.
To produce the new linked list again from the file you can use fscanf() in a loop, but make sure you allocate memory for your strings when you copy them from the file.
I'm new to C programming, and I'm having some difficulties. I'm trying to re-code to use a linked list to store the structs you read from the file, but I can't understand how to save the text line for line and add it to the linked list.
This is my code:
#include<stdio.h>
#include<stdlib.h>
#include"Cars.h"
void write_to_file() {
FILE* file = NULL;
file = fopen("Cars.dat", "wb");
Car c = { "Toyota", "Civic", "black", 5, 50000 };
size_t ret = fwrite(&c, sizeof c, 1, file);
Car d = { "Toyota1", "Civic1", "Red", 2, 55000 };
ret = fwrite(&d, sizeof d, 1, file);
fclose(file);
}
void print_cars(Car *c, int count) {
FILE* f = fopen("output.txt", "w");
for (int i = 0; i < count; i++) {
fprintf(f, "%s %s %s %d %f\n", c[i].model, c[i].manufacturer, c[i].color, c[i].seatCapacity, c[i].price);
}
fclose(f);
}
void read_from_file() {
Car c;
size_t SIZE = sizeof c;
size_t ret = 1;
FILE* file = NULL;
file = fopen("Cars.dat", "rb");
int count = 0;
do{
ret = fread(&c, sizeof c, 1, file);
if (ret != 1)
break;
count++;
//printf("%s %s %s %d %f\n", c.model, c.manufacturer, c.color, c.seatCapacity, c.price);
}while(!feof(file));
fclose(file);
if (count == 0) {
printf("No cars in the file provided\n");
return;
}
file = fopen("Cars.dat", "rb");
Car* cars_array = NULL;
cars_array = (Car *)malloc(count * SIZE);
ret = fread(cars_array, SIZE*count, 1, file);
//printf("%d\n", ret);
print_cars(cars_array, count);
}
int main()
{
//write_to_file();
read_from_file();
return 0;
}
Struct code:
#ifndef Cars_h
#define Cars_h
typedef struct cars {
char manufacturer[35]; //toyota, honda
char model[35]; //camry, civic
char color[20]; //black, red
int seatCapacity; //4,5
float price; //20k, 25k
}
Car;
#endif
If anyone could give me some pointers on what I need to do next to make it work, I would highly appreciate it. Thank you
You could implement a linked list of 'Car' objects by adding a next pointer to the Car structure. For example:
typedef struct Car {
/* existing fields */
char model[LLL];
char manufacturer[MMM];
char color[CCC];
int seatCapacity;
float price;
struct Car *next; /* new member for linked list */
} Car;
(An assumption is made about Car as source code for Cars.h was not provided.)
You would have to account for this extra field in Cars.dat (where it could be stored with a NULL value assuming that the file has just an array and no notion of a linked list).
When reading, you could malloc space for each Car separately, fread it into the malloc-ed buffer, and update the previous' Car's next pointer to point to the newly loaded Car. In this manner you can build up a simple linked list of Cars.
p.s. I've never heard of a Toyota Civic :)
You can create a new struct as below which includes your Car struct as a field. And a pointer to the next node.
typedef struct {
Car payload;
CarNode* nextNode;
}CarNode
And you don't need to calculate the count of records in the file. Just read one and malloc a CarNode and use the CarNode.payload field as the buffer to hold the content read from the file. Something like below snippet:
CarNode head, tail; // Suppose you are using single linked list.
/*
Initialize the head and tail pointer. At first, they should both point to the first node.
*/
...
/*
Allocate a new node while reading a record from the file.
*/
CarNode *p = (CarNode *)malloc(sizeof(CarNode));
ret = fread(&(p->payload), sizeof(Car), 1, file);
/*
Insert the newly read node into the link list.
*/
tail->nextNode = p;
tail = p;
We are told our input file would be a simple list of numbers:
1 3 4
2 3
3 4
4 1 2
Where the first number is the source node, and the proceeding numbers are it's adjacent nodes.
I am trying to figure out how to best store this.
I wanted to firstly initialize a "graph", an array that contains all these nodes.
Then upon reading the file, line by line, I would store the root node into the graph array, and then update the node's outlist (adjacent nodes) with the following numbers until we reach the end of the line, repeating this for each line until EOF.
However I'm struggling on how to initialize the graph, do I just assume a certain size and realloc() once the size is hit? Do I read the file first and count the number of lines to find out the size, then re-read the file to store the nodes? Is there any other way?
Here is the code for my data structures:
int initialize (Graph *mygraph, int MaxSize) {
mygraph->MaxSize = MaxSize;
mygraph->table = (Node *)malloc(sizeof(Node) * MaxSize);
return 0;
}
int insert_node (Graph *mygraph, int n, char *name) {
mygraph->table[n].name = strdup(name);
mygraph->table[n].outdegree = 0;
return 0;
}
int insert_link (Graph *mygraph, int source, int target) {
List *newList = (List *)malloc(sizeof(List));
newList->index = target;
newList->next = mygraph->table[source].outlist;
mygraph->table[source].outlist = newList;
return 0;
}
So upon reading the file,
I initialize the graph.
I read the first number, store it as a new graph node.
I read the next numbers until hitting "\n", and store these as graph links to the above root node.
I do this for each line until hitting EOF.
As you can see I have no idea what the "MaxSize" until the whole file is read.
Thanks!
I'm rather new to C so sorry if I've done anything silly.
You could have some initial guess for MaxSize (e.g. 8) and grow when needed your data (perhaps by graph->MaxSize += graph->MaxSize/2) using realloc, or just by malloc-ing a bigger new chunk, copying the older chunk inside, then free-ing that older chunk). Don't forget to check the successful result of any malloc or calloc or realloc call, they could (rarely) fail.
Notice that I have no idea of how your Graph and Node type is declared (just guessing).
I am assuming and guessing you have declared something like
typedef struct node_st Node;
typedef struct graph_st Graph;
struct node_st {
char*name; // strdup-ed
unsigned outdegree;
};
struct graph_st {
unsigned MaxSize;
Node* table; //calloc-ed, of allocated size MaxSize
};
So for example your insert_node function might be
void insert_node (Graph *mygraph, int n, char *name) {
assert (mygraph != NULL);
assert (n >= 0);
assert (name != NULL && *name != (char)0);
unsigned maxsize = mygraph->MaxSize;
if (maxsize <= n) {
unsigned newmaxsize = n + maxsize/2 + 1;
Node* newtable = calloc (newmaxsize, sizeof(Node));
if (!newtable)
perror("growing table in graph"), exit(EXIT_FAILURE);
for (unsigned i=0; i<maxsize; i++)
newtable[i] = mygraph->table[i];
free (mygraph->table);
mygraph->table = newtable;
mygraph->MaxSize = newmaxsize;
};
mygraph->table[n].name = strdup(name);
mygraph->table[n].outdegree = 0;
}
You probably don't need insert_node to return a value (otherwise you won't always return 0). So I made it a void returning function (i.e. a "procedure" or "routine").
Whew! Long title...here's some pseudo-code to explain that verbiage:
int main(){
int* ptr = function1(); //the data that ptr points to is correct here
function2(ptr);
}
int function2(int* ptr){
//the data that ptr points to is still correct
int i;
for(i=0;i<length;printf("%d\n", (*ptr)[i]), i++); //since ptr points to a contiguous block of memory
function3(ptr);
}
int function3(int* ptr){
//the data that ptr points to is INCORRECT!!!
}
Why would the data in function3 be incorrect?
Note: function1 performs a malloc() and returns the pointer to that memory.
ACTUAL CODE
#include <stdlib.h>
#include <stdio.h>
//Structures
struct hash_table_data_
{
int key, data;
struct hash_table_data_ *next, *prev;
};
struct hash_table_
{
int num_entries;
struct hash_table_data_ **entries;
};
typedef struct hash_table_data_ hash_table_data;
typedef struct hash_table_ hash_table;
//Prototypes
hash_table *new_hash_table(int num_entries);
int hash_table_add(hash_table *ht, int key, int data);
int hash_table_loader(hash_table* ht);
//Main
int main()
{
int num_entries = 8;//THIS MUST BE AUTOMATED
hash_table* ht = new_hash_table(num_entries);
hash_table_loader(ht);
return 0;
}
//Function Definitions
hash_table *new_hash_table(int num_entries)
{
hash_table* ht = (hash_table*) malloc(sizeof(hash_table));
hash_table_data* array = malloc(num_entries * sizeof(hash_table_data));
int i;
for (i=0;i<num_entries;i++)
{
array[i].key = -1;
array[i].data = -1;
array[i].next = NULL;
array[i].prev = NULL;
}
ht->entries = &array;
ht->num_entries = num_entries;
return ht;
}
int hash_table_add(hash_table *ht, int key, int data)
{
//VERIFY THAT THE VALUE ISN'T ALREADY IN THE TABLE!!!!!!!!!!!
int num_entries = ht->num_entries;
hash_table_data* array = *(ht->entries); //array elements are the LL base
int hash_val = key%num_entries;
printf("adding an element now...\n");
printf("current key: %d\n", array[hash_val].key);
int i;
for(i=0;i<num_entries;printf("%d\n", (*(ht->entries))[i].key),i++);//DATA IS INCORRECT!!!!
if (array[hash_val].key == -1)//is this the base link?
{
printf("added a new base link!\n");
array[hash_val].key = key;
array[hash_val].data = data;
array[hash_val].next = NULL;
array[hash_val].prev = &(array[hash_val]);
}
else//since it's not the base link...do stuff
{
hash_table_data* new_link = malloc(sizeof(hash_table_data));
new_link->key = key;//set the key value
new_link->data = data;//set the data value
if (array[hash_val].next == NULL)//we must have the second link
{
printf("added a new second link!\n");
new_link->prev = &(array[hash_val]); //set the new link's previous to be the base link
array[hash_val].next = new_link; //set the first link's next
}
else//we have the 3rd or greater link
{
printf("added a new 3rd or greater link!\n");
hash_table_data next_link_val = *(array[hash_val].next);
while (next_link_val.next != NULL)//follow the links until we reach the last link
{
next_link_val = *(next_link_val.next);//follow the current link to the next
}
//now that we've reached the last link, link it to the new_link
next_link_val.next = new_link; //link the last link to the new link
new_link->prev = &(next_link_val); //link the new link to the last link
}
}
return 0;
}
int hash_table_loader(hash_table* ht)
{
int i;
for(i=0;i<(ht->num_entries);printf("%d\n", (*(ht->entries))[i].key),i++); //DATA IS STILL CORRECT HERE
FILE *infile;
infile = fopen("input.txt", "r");
while(!feof(infile))
{
int key,data;
fscanf(infile, "%d %d", &key, &data);
hash_table_add(ht, key, data);
}
fclose(infile);
}
Note: Issue occurring the first time hash_table_add() is called.
Your first problem is here:
ht->entries = &array;
You cause the structure to hold a hash_table_data** which points to the variable hash_table_data* array which is local to the function; then you exit the function and return a pointer to the structure. The structure still exists (it was allocated via malloc(), and the stuff that array points to still exists, but array itself does not. Accordingly, this pointer within the structure is now invalid.
As far as I can tell, there is no reason for you to be holding a pointer-to-pointer here. Just use hash_table_data* as the entries type, and copy array into that struct member. Pointers are values too.
I guess you iterate incorrectly
for(i=0;i<length;printf("%d\n", (*ptr)[i]), i++);
this is nonsense.
You should rewrite it as this:
for(i=0;i<length;i++)
printf("%d\n", ptr[i]);
(*ptr)[i] is just wrong, it doesn't make sense if you think about it.
*ptr is the first element of the pointed-to array of ints.
ptr[i] is thi ith one, this is what you need.
Please, read Section 6 carefully.
A couple of advises based on this question:
Don't write overcomplicated code like this for statement with comma operator used, it just rarely needed and leads not only to confusion, but to mistakes (although no mistakes with it in this particular example)
Look carefully for mistakes, don't blame everything on functions. If your code doesn't work, try finding the exact place which is wrong and prove it. In this example people who tested your code were right: functions are definitely not the cause of the error.
hash_table *new_hash_table(int num_entries)
{
hash_table* ht = (hash_table*) malloc(sizeof(hash_table));
hash_table_data* array = malloc(num_entries * sizeof(hash_table_data));
// ....
ht->entries = &array; // Problem
// ...
return ht;
} // Life time of array ends at this point.
You are taking the reference of the local variable array and assigning it to ht->entries which is no more valid once the function returns.