I have spent quite a lot time attempting to find a solution how to write and read structure to/from file and completely get stuck.
The code should be very simple, but I think I've missed quite important details about fwrite/fread. I think write is working but I am not completely sure. Once I try to read into array I get segmentation fault error on the k=1.
I would be ultimately happy if someone could guide me in what direction should I move further.
#include <stdio.h>
struct Student {
char matrikl[6];
char shortName[6];
int value1;
int value2;
int value3;
int value4;
int money;
}xStudent;
int calcMoney(int,int,int,int);
int main(void)
{
int i=0;
printf("How much students to insert!\n");
scanf("%i",&i);
struct Student S[i];
for (int var = 0; var < i; ++var) {
printf("insert student data\n");
printf("Matriklinumber\n");
scanf("%s", S[var].matrikl);
printf("Student name\n");
scanf("%s", S[var].shortName);
printf("Insert values!\n");
scanf("%i%i%i%i",&S[var].value1,&S[var].value2,&S[var].value3,&S[var].value4);
S[var].money=calcMoney(S[var].value1,S[var].value2,S[var].value3,S[var].value4);;
}
FILE*sourcefile;
sourcefile=fopen("text.txt","ab+");
for (int var = 0; var < i; ++var) {
fwrite(&S[var],sizeof(struct Student),1,sourcefile);
}
fclose(sourcefile);
sourcefile=fopen("text.txt","rb+");
struct Student A[i];
if (sourcefile==NULL) perror ("Error opening file");
else
{
int k=0;
while (fgetc(sourcefile) != EOF) {
fread(&A[k],sizeof(struct Student),1,sourcefile);
k++;
}
}
fclose(sourcefile);
return 0;
}
int calcMoney(int xvalue1, int xvalue2, int xvalue3, int xvalue4)
{
int Sum = xvalue1+xvalue2+xvalue3+xvalue4;
if(Sum==20)
return 100;
else
if(Sum>=16 && Sum<20)
return 75;
else return 0;
}
You have the right idea, but this should fix your problem.
for (int var = 0; var < i; ++var) {
fwrite(&S[i], sizeof(struct Student), 1, sourcefile);
}
The argument 1 in fwrite after the sizeof statement indicates you only want to add one Student at a time.
Please see the doc for fwrite: http://www.cplusplus.com/reference/cstdio/fwrite/
And also:
while (fgetc(sourcefile) != EOF) {
fread(&A[k], sizeof(struct Student), 1, sourcefile);
k++;
}
Please see the doc for fread: http://www.cplusplus.com/reference/cstdio/fread/
I know it's the cplusplus website, but it's the only place I could find good docs on the functions.
Also, the below array initializes to 0 elements:
struct Student A[k];
You may want to try managing a linked list for this instead.
Might end up looking something like:
struct StudentList {
struct Student* student;
struct StudentList* next;
};
struct StudentList* students = NULL;
while(!feof(sourcefile)) {
struct StudentList* newlink = malloc(sizeof(struct StudentList));
newlink->student = malloc(sizeof(struct Student));
newlink->next = NULL;
fread(newlink->student, sizeof(struct Student), 1, sourcefile);
if(NULL != students) {
struct StudentList* iter;
for(iter=students;
NULL != iter->next;
iter = iter->next); // iterate to last link
iter->next = newlink; // add new link to the end
} else students = newlink; // add new link to the beginning
}
Then to free your list, just use something like:
struct StudentList* iter, *head;
for(iter=students;
NULL != iter;)
{
head = iter;
free(iter->student);
free(iter);
iter = head;
}
the error is in the declaration of array A.
struct Student A[k];
k=0; so you are getting error since A[1] or so the array is not defined.
change the declaration to
struct Student A[i];
then it should run if rest of code and syntax is fine.
Fix fwrite line to:
fwrite(&S[i], sizeof(struct Student), 1, sourcefile);
Related
I've been trying to implement a linked list for a classroom roster in the format
Total Students=
(i)Students name="ABCS"
Total lectures taken =n
Lectures taken="Physics", "Chemistry" ....... n-1
The code executes for the first node, takes input for classTaken[ ] and then just exits taking 1 less than the required inputs, no SIGSEGV nothing, it doesnt return to main.
If I comment out classTaken the students name and Total lectures (noClass) work for noOfStudents but throw a SIGSEGV after finishing
. I think the return address is being overwritten ,maybe due to wrong allocation .....
Can someone please check out my mallocs to see if ive allocated correctly
PS: Ive commented out the printf statements ive used for debugging
EDIT: OK I've made some changes :
changed int to size_t in loops and variables: noClass and noOfStudents
put flexible array to end of struct
removed casts for allocations
malloc'd new->name before scanf
Error still remains (ie program exits after taking 1st input of classTaken[] for first node) I also managed to zero in on the error
//writes classes taken by students array of strings
for(size_t j=0; j<(new->noClass); j++){
printf("\nEnter class(%d) = ",j+1);
scanf("%s",new->classTaken[j]);
}
Above loop fails to write to location scanf returns 0, perror displays "illegal seek"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void prgmSt(void);
#define SUB_MAX 13
struct student{
char *name;
int noClass;
struct student *next;
char *classTaken[];
};
struct student **head;
char **studentNames;
size_t noOfStudents = 0;
//Makes Single Linked List having noOfStudents nodes
void prgmSt(void){
struct student **tmp;
struct student **tmp1;
for(size_t i=0; i<noOfStudents; i++){
//printf("\nhere1\n");
struct student *new = malloc(sizeof(struct student));
//printf("\nhere2\n");
if(new==NULL){
perror("E:");
exit(EXIT_FAILURE);
}
//printf("\nhere3\n");
//printf("new=%p\n",new);
printf("\nEnter the name of Student(%d)= ",i+1);
new->name=malloc(128);
scanf("%s", new->name);
studentNames[i] = new->name;
//printf("\n%s, test1 ", studentNames[i]);
printf("\nEnter the number of Classes he attended= ");
scanf("%zu",&(new->noClass));
//printf("\nhere4\n");
//printf("%d, test2 \n", new->noClass);
new->classTaken = malloc((new->noClass)*SUB_MAX); //Assumed MAX_LIMIT
//of 12+1 characters for
//sub name
if(new->classTaken==NULL){
exit(EXIT_FAILURE);
}
new->next = NULL;
//writes classes taken by students array of strings
for(size_t j=0; j<(new->noClass); j++){
printf("\nEnter class(%d) = ",j+1);
scanf("%s",new->classTaken[j]);
}
if(i==0){
head = &new;
tmp = &new;
//printf("head=%p tmp=%p new=%p \n",head,tmp,new);
} else{
//printf("here5\n");
tmp1 = &new;
(*tmp)->next = *tmp1;
//printf("here6\n");
//printf("tmp->next=%p new=%p \n",(*tmp)->next,new);
//printf("here7\n");
}
new = new->next;
//printf("new=%p\n",new);
}
}
int main(void){
setbuf(stdout, NULL);
printf("What is the no of students in class:: ");
scanf("%d", &noOfStudents);
studentNames=malloc(noOfStudents*sizeof(char *));
if(studentNames==NULL){
perror("E:");
exit(EXIT_FAILURE);
}
prgmSt();
//printf("succ");
return EXIT_SUCCESS;
}
EDIT : Ive resolved the problem there were 3 main problems(it was a mess)
exactly as i had been told, memory was not allocated to each individual string before scanf
tried to use 2 flexible arrays inside a struct
addresses were being wrongly being assigned to the nodes
I split the structs into student and details, resolved the address assignment, improved readability, and error control ...
#define MAX_LENGTH 13
struct student{
size_t noClass;
char **classTaken;
};
struct node{
struct student* details;
struct node *next;
char *name;
};
struct node **head;
char **studentNames;
size_t noOfStudents = 0;
void prgmSt(void){
struct node **tmp;
int testVar = 0;
for(size_t i=0; i<noOfStudents; i++){
struct node * studenti = malloc(sizeof(struct node));
studenti->details = malloc(sizeof(struct student));
if((studenti==NULL)|(studenti->details==NULL)){
perror("E:");
exit(EXIT_FAILURE);
}
printf("\nEnter the name of Student(max 12)(%zu)= ",i+1);
studenti->name=malloc(MAX_LENGTH);
if(studenti->name==NULL){
perror("E:");
exit(EXIT_FAILURE);
}
testVar=scanf("%s", studenti->name);
if(testVar!=1){
perror("E:");
exit(EXIT_FAILURE);
}
studentNames[i] = studenti->name;
printf("\nEnter the number of Classes he attended= ");
testVar = scanf("%zu",&(studenti->details->noClass));
if(testVar!=1){
perror("E:");
exit(EXIT_FAILURE);
}
studenti->details->classTaken =
malloc((studenti->details- >noClass)*sizeof(char *)); //Assumed MAX_LIMIT of
//12+1 characters for sub name
if(studenti->details->classTaken==NULL){
exit(EXIT_FAILURE);
}
studenti->next = NULL;
for(int j=0; j<(studenti->details->noClass); j++){
studenti->details->classTaken[j] =
malloc(MAX_LENGTH*sizeof(char));
if(studenti->name==NULL){
perror("E:");
exit(EXIT_FAILURE);
}
printf("\nEnter class(%d)(MAX 12 CHARACTERS) = ",j+1);
testVar=scanf("%s",studenti->details->classTaken[j]);
if(testVar!=1){
perror("E:");
exit(EXIT_FAILURE);
}
}
if(i==0){
head = &studenti;
tmp = &studenti;
} else{
(*tmp)->next = studenti;
tmp = &studenti;
}
studenti = studenti->next;
}
}
I'm trying to make a code that will allow the user to input any amount of entries he wants, and then print them out (and other functions, still have to get to that). But when I try to launch the code it allows me to input the entries, but when I want to print them out, it doesn't register current.name or current.telNo (only prints out 1: has tel. No. ) and a segmentation error follows after it. Any idea how I can fix that.
#include <stdio.h>
#include <stdlib.h>
int listSize;
int counter = 0;
struct Entry {
char name[20];
char telNo[9];
struct Entry *next;
} current;
int main()
{
struct Entry *linkedList = (struct Entry * )malloc(sizeof(struct Entry));
struct Entry *current = linkedList;
first(current);
print(current);
return 0;
}
void first(struct Entry linkedList)
{
int i;
printf("enter list size: ");
scanf("%d", &listSize);
printf("Now enter entries one by one: \n");
for (i = 0; i < listSize; i++) {
counter++;
printf("Name: ");
scanf("%s", linkedList.name);
printf("Telephone: ");
scanf("%s", linkedList.telNo);
if (i != listSize -1) {
linkedList.next = (struct Entry *)malloc(sizeof(struct Entry));
linkedList = *linkedList.next;
} else {
linkedList.next = NULL;
}
}
}
void print(struct Entry linkedList)
{
int nr = 1;
printf("\nTelephone book is:\n");
while (current.name != NULL) {
printf("%d: %s has tel. No.\t%s\n", nr, current.name, current.telNo);
current = *current.next;
nr++;
}
}
Instead of . you should have used -> and in your print() you were traversing current instead of linkedList which was causing the issue. Also your functions definitions should come before its usage. Please check the below snippet, i have made the corresponding changes.
#include <stdio.h>
#include <stdlib.h>
int listSize;
int counter = 0;
struct Entry {
char name[20];
char telNo[9];
struct Entry *next;
} current;
void print(struct Entry *linkedList)
{
int nr = 1;
printf("\nTelephone book is:\n");
while (linkedList->name != NULL) {
printf("%d: %s has tel. No.\t%s\n", nr, linkedList->name, linkedList->telNo);
linkedList = linkedList->next;
nr++;
}
}
void first(struct Entry *linkedList)
{
int i;
printf("enter list size: ");
scanf("%d", &listSize);
printf("Now enter entries one by one: \n");
for (i = 0; i < listSize; i++) {
counter++;
printf("Name: ");
scanf("%s", linkedList->name);
printf("Telephone: ");
scanf("%s", linkedList->telNo);
if (i != listSize -1) {
linkedList->next = (struct Entry *)malloc(sizeof(struct Entry));
linkedList = linkedList->next;
} else {
linkedList->next = NULL;
}
}
}
int main()
{
struct Entry *linkedList = (struct Entry * )malloc(sizeof(struct Entry));
struct Entry *current = linkedList;
first(current);
print(current);
return 0;
}
while (current.name != NULL)
should be
while (current != NULL)
otherwise current = *current.next will become NULL and crash on the while condition
current.name is a pointer to reserved 20-byte area, which is always non-NULL.
I would initialize *next with NULL every time I allocate it (there are few ways to do that, easiest: assign NULL to it right after malloc). And do the check like that: if (current.next != NULL).
You also can check for current.name[0] != 0, but the first option is cleaner.
There are a number of errors in your code but, essentially two main 'points of confusion':
First, you seem to be confusing a structure (struct Entry) with a pointer-to-structure (as in, for example, struct Entry *next;).
Second, you have two different variables called current - one defined globally (which is the only one that is 'visible' to your print function), and another one defined locally inside main (this one will 'hide' the former).
Here is a corrected version of your code, with triple-slash (///) comments wherever I've made changes. Feel free to ask for further clarification and/or explanation.
#include <stdio.h>
#include <stdlib.h>
int listSize;
int counter = 0;
struct Entry {
char name[20];
char telNo[9];
struct Entry* next;
} *current; /// Here, we define a GLOBAL variable, "current" that is a POINTER to the struct
void first(struct Entry* linkedList); /// Put "Function Prototypes* here, so that "main" knows what they are
void print(struct Entry* linkedList); /// You need to pass POINTERS to structures, not the actual structures
int main()
{
struct Entry* linkedList = (struct Entry*)malloc(sizeof(struct Entry));
// struct Entry* current = linkedList; /// This declaration 'hides' the global variable
current = linkedList; /// Here, we (properly) assign the global pointer's value
first(current);
print(current);
return 0;
}
void first(struct Entry* linkedList) /// Pointer (see above)
{
int i;
printf("enter list size: ");
scanf("%d", &listSize);
printf("Now enter entries one by one: \n");
for (i = 0; i < listSize; i++) {
counter++;
printf("Name: ");
scanf("%s", linkedList->name); /// Use "->" instead of "." to get a pointer's member (and elsewhere)
printf("Telephone: ");
scanf("%s", linkedList->telNo); /// See above
if (i != listSize - 1) {
linkedList->next = (struct Entry*)malloc(sizeof(struct Entry)); /// See above
linkedList = linkedList->next; /// Again - changed here to use pointers!
}
else {
linkedList->next = NULL; /// See above
}
}
}
void print(struct Entry* linkedList) /// Pointer (see above)
{
int nr = 1;
printf("\nTelephone book is:\n");
while (current != NULL) { /// You need to check if "current" is not NULL before trying to access any of its members...
// while (current.name != NULL) {
printf("%d: %s has tel. No.\t%s\n", nr, current->name, current->telNo); /// Same "." to "->" changes as before
current = current->next; /// And again!
nr++;
}
}
There are other ways to 'fix' your code (and also improve it): for example, you never use the argument you pass to print, relying instead on the 'global' variable I mentioned earlier.
I'm trying to setup a graph in C. I tried the graph with user input and it works perfectly. However, i am trying to implement a read from file. The last else statement is where the error is coming from because when i commented it out it compiles without any problems. I have included a comment over the block i think that has the problem. Please let me know if there is anything else needed for this question.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node* next;
};
//int counter and mainVertex would be used to determine if graph is connected.
// void graphConnection(){
//
//
//
//
//
//
// }
char* deblank(char* input)
{
int i,j;
char *output=input;
for (i = 0, j = 0; i<strlen(input); i++,j++)
{
if (input[i]!=' ')
output[j]=input[i];
else
j--;
}
output[j]=0;
return output;
}
struct node *G[1000];
int counter = 0;
char *mainVertex;
void readingEachLine(){
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
//Read file and exit if fail
fp = fopen("test.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
line = deblank(line);
int i = 0;
struct node* cursor = malloc(sizeof(struct node));
struct node* secondcursor = malloc(sizeof(struct node));
struct node* tempitem;
while(line[i] != '\n'){
//If its the first of the line look into the array and set struct cursor to the corresponding
//array position
if (i == 0){
mainVertex[counter] = line[0];
int convertor = line[i] - '0';
cursor = G[convertor];
counter++;
}
//if its not the first, then set a struct with that number as data
else{
tempitem = malloc(sizeof(struct node));
int convertor = line[i] - '0';
tempitem->data = convertor;
tempitem->next = NULL;
}
//if there is no element connected to the struct in array, connect the tempitem
if (cursor->next == NULL){
cursor->next = tempitem;
}
//If there are already connected elements, loop until the end of the linked list
//and append the tempitem
//ERROR: I GET SEGMENTATION FAULT FROM HERE. TRIED AFTER COMMENTING IT OUT
else{
secondcursor = cursor;
while(secondcursor->next != NULL){
secondcursor = secondcursor->next;
}
secondcursor->next = tempitem;
}
i++;
}
printf("\n");
}
}
int main(void){
for (int i = 1; i < 1000; i++)
{
G[i]= malloc(sizeof(struct node));
G[i]->data = i;
G[i]->next = NULL;
}
readingEachLine();
}
EDIT: This is how the text file looks like:
1 3 4
2 4
3 1 4
4 2 1 3
Your code has several misconceoptions:
Apparently, you can have a maximum of 1,000 nodes. You have an array G of 1,000 head pointers to linked lists. Don't allocate memory for all 1,000 nodes at the beginning. At the beginning, all lists are empty and an empty linked list is one that has no node and whose head is NULL.
In your example, cursor is used to iterate oer already existing pointers, so don't allocate memory for it. If you have code like this:
struct node *p = malloc(...);
// next use of p:
p = other_node;
you shouldn't allocate. You would overwrite p and lose the handle to the allocated memory. Not all pointers have to be initialised with malloc; allocate only if you create a node.
Your idea to strip all spaces from a line and then parse single digits will fail if you ever have more then 9 nodes. (But you cater for 1,000 node.) Don't try to parse the numbers yourself. There are library functions for that, for example strtol.
It is not clear what mainVertex is supposed to be. You use it only once, when you assign to it. You treat it like an array, but it is a global pointer, initialised to NULL. When you dereference it, you get undefined behaviour, which is where your segmentation fault probably comes from.
Here's a program that does what you want to do. (It always inserts nodes at the head for simplicity and it should have more allocation checks.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
maxNodes = 1000
};
struct node{
int data;
struct node* next;
};
struct node *G[maxNodes];
size_t nnode = 0;
int read_graph(const char *fn)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
fp = fopen(fn, "r");
if (fp == NULL) return -1;
while (getline(&line, &len, fp) != -1) {
char *p;
char *end;
int id;
int n;
id = strtol(line, &end, 10);
if (end == line) continue;
if (id < 1 || id > maxNodes) break;
if (id > nnode) nnode = id;
id--;
p = end;
n = strtol(p, &end, 10);
while (p != end) {
struct node *nnew = malloc(sizeof(*nnew));
nnew->data = n - 1;
nnew->next = G[id];
G[id] = nnew;
p = end;
n = strtol(p, &end, 10);
}
}
fclose(fp);
free(line);
return 0;
}
int main(void)
{
if (read_graph("test.txt") < 0) {
fprintf(stderr, "Couldn't gread raph.\n");
exit(1);
}
for (int i = 0; i < nnode; i++) {
struct node *p = G[i];
if (p) {
printf("%d:", i + 1);
for (; p; p = p->next) {
printf(" %d", p->data + 1);
}
puts("");
}
}
for (int i = 0; i < nnode; i++) {
struct node *p = G[i];
while (p) {
struct node *old = p;
p = p->next;
free(old);
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
struct date {
int year;
int month;
int day;
};
struct person{
char name[64];
struct date birthday;
};
struct aop {
int max;
struct person **data;
};
struct aop *create_aop(int max) {
struct aop *new = malloc(sizeof(struct aop));
new->data = malloc(max*sizeof(struct person));
for (int i=0; i<max; i++) {
new->data[i] = NULL;
}
new->max = max;
return new;}
void destroy_aop(struct aop *a) {
free(a->data);
free(a);
}
int add_person(struct aop *a, char *name, struct date birthday) {
if (a->data[a->max-1] != NULL) {
return -1;
}
struct person *new_person = malloc(sizeof(struct person));
strcpy(new_person->name, name);
new_person->birthday = birthday;
for (int i=0; i<a->max; i++) {
if (a->data[i] == NULL) {
a->data[i] = new_person;
break;
}
}
free(new_person);
return 0;
}
I have some questions about the code I wrote. First, do I need to add extra codes into create_aop to initialize name and birthday inside of person? And I found that after the free(new_person) in add_person, I cannot reach a->data[0]->name. How can I change the of a->data[i] without using another pointer?
struct aop *birthdays(const struct aop *a, int month) {
int m = a->max;
struct aop *n = create_aop(m);
int j = 0;
for (int i=0; i<m; i++) {
if (a->data[i] != NULL) {
if (a->data[i]->birthday.month == month) {
n->data[j] = a->data[i];
j++;
}
} else {
break;
}
}
if (j == 0) {
return NULL;
}
return n;
}
Every time I run the function above, there are some errors memory. I have been thinking about it for hours but have no idea what is wrong in this one.
There are two big mistakes in this code. First, the allocation of your data array in struct aop is flawed.
new->data = malloc(max*sizeof(struct person));
You don't want it to point to memory of size max times length of a person struct, do you? Since it is a pointer to pointers, it's size only has to be max times length of a pointer, i.e.
new->data = malloc(max*sizeof(struct person*));
You could also let data directly point to struct person. Then the first line would be correct and you don't have to allocate memory each time you create a new person. Instead, you would just use the memory that data points to:
(aop->data[i]).name = ...
and so on.
Secondly, you are freeing your person struct right after creation.
free(new_person);
Now aop->data[i] is a dangling pointer, because the address it points to could be overwritten any time (because it's not locked by malloc anymore). Instead, you have to free it in your destroy function. It might look something like this:
void destroy_aop(struct aop *a) {
int i;
for(i = 0; i < a->max; i++)
{
if(a->data[i] != NULL) {
free(a->data[i]);
}
}
free(a->data);
free(a);
}
I am trying to implement a hash table with linked list chaining. The following code below works -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABSIZ 200
struct record {
struct record *next;
char name[BUFSIZ];
int data;
};
static struct record *htable[TABSIZ];
unsigned hash(char *s)
{
unsigned h;
for (h = 0; *s; s++)
h = *s;
//printf("%d", h%TABSIZ);
//I know its not a good hash function but i wanted to check chaining
return h % TABSIZ;
}
struct record *find(char *name)
{
struct record *item;
for (item = htable[hash(name)]; item; item = item->next)
{
if (strcmp(name, item->name) == 0)
return item;
}
return NULL;
}
struct record *insert(char *name,int value)
{
struct record *item;
unsigned h;
if ((item = find(name)) == NULL)
{
if ((item = malloc(sizeof (*item))) == NULL)
return NULL;
strcpy(item->name, name);
item->data=value;
h = hash(name);
item->next = htable[h];
htable[h] = item;
}
return item;
}
void printTable()
{
int i=0;
struct record *temp;
for(i=0;i<=TABSIZ;i++)
{
temp=htable[i];
while(temp!=NULL)
{
printf("\n%d - %s - %d\n", i,temp->name, temp->data);
temp=temp->next;
}
}
}
int main(void)
{
char buf[BUFSIZ];int value;
struct record *item;
do{
printf("Enter the name of the student:\n");
scanf("%s", buf);
if(strcmp(buf,"stop")==0) break;
printf("Enter the marks of the student:\n");
scanf("%d", &value);
if(insert(buf, value)==NULL)
{
break;
}
}while((strcmp(buf,"stop"))!=0);
printf("Enter a name to find: ");
scanf("%s", buf);
if((item=find(buf))!=NULL)
printf("The marks of the student is %d\n", item->data);
else printf("\n Not Found\n");
printTable();
return 0;
}
Now I am trying to remove the global variable and use local variable for the array of structures. I removed the global declaration of htable and declared it in main as
struct record *htable[TABSIZ];
and changed the functions to
struct record *find(struct record *htable, char *name);
struct record *insert(struct record *htable, char *name,int value);
and I'm calling the functions as
find(htable, name);
insert(htable,name,value);
but now my program is segfaulting. Am i passing the array of structures right? and have I declared it correctly. Any help would be greatly appreciated.
I was going down the wrong path with my earlier answer.
When it's a global, it's automatically initialized to 0.
When it's declared on the stack of main, it's not initialized.
Add a memset( htable, 0, sizeof(htable)) in main(), and that should return it to the previous behavior.
In printTable():
for(i=0;i<=TABSIZ;i++)
looks suspect. You prabably want:
void printTable()
{
unsigned int i;
struct record *temp;
for(i=0; i < TABSIZ;i++)
{
for (temp=htable[i]; temp!=NULL; temp=temp->next )
{
printf("\n%d - %s - %d\n", i,temp->name, temp->data);
}
}
}