I'm writing a header files and I must be allocating the space wrong because I'm trying to do a linked list but the nodes keep changing.
table.h (some of these I haven't written yet)
Code:
#ifndef TABLE_H_
#define TABLE_H_
#include <stdio.h>
struct lineNode;
struct entry;
struct table;
/* returns an empty table */
struct table * newTable ( );
/* returns the result of adding a definition for the given symbol
in the line with the given number to the table */
struct table * addDef (char * symbol, int lineNum, struct table * symbols);
/* returns the result of adding a use of the given table in the line
with the given number to the table */
struct table * addUse (char * symbol, int lineNum, struct table * symbols);
/* print the table, in the format described above, to the given file */
void printTable (struct table * symbols, FILE * out);
#endif /* TABLE_H_ */
table.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "table.h"
struct lineNode {
int lineoccur;
struct lineNode *next;
};
struct entryNode {
struct entryNode *next;
struct entry *row;
};
struct entry {
char * symbol;
int linedef;
struct lineNode *occur;
};
struct table {
int entrycount;
struct entryNode *first;
};
/* returns an empty table */
struct table * newTable( ) {
struct table * table1 = malloc(sizeof(struct table));
table1->entrycount = 0;
return table1;
}
struct table *firstSymbol(struct table * symbols) {
printf("the symbol is %s\n", symbols->first->row->symbol);
}
/* returns the result of adding a definition for the given symbol
in the line with the given number to the table */
struct table * addDef (char * symbol, int lineNum, struct table * symbols){
printf("adding %s\n", symbol);
printf("entrycount is %d\n", symbols->entrycount);
if (symbols->entrycount == 1)
printf("should be word1 %s\n", symbols->first->row->symbol);
struct entry *newEntry = malloc(sizeof(struct entry));
newEntry->symbol = symbol;
newEntry->linedef = lineNum;
struct entryNode *newEntryNode = malloc(sizeof(struct entryNode));
newEntryNode->row = &newEntry;
//somewhere here the symbol in the first node changed to "word2"
if (symbols->entrycount == 1)
printf("again should be word1 %s\n", symbols->first->row->symbol);
if (symbols->entrycount == 0) {
symbols->first = &newEntryNode;
symbols->entrycount = 1;
printf("should be word1 %s\n", symbols->first->row->symbol);
}
else {
printf("2should be word1 %s\n", symbols->first->row->symbol);
struct entryNode *nEntryNode = malloc(sizeof(struct entryNode));
nEntryNode = symbols->first;
int s = symbols->entrycount;
while (s != 1) {
nEntryNode = nEntryNode->next;
s--;
}
nEntryNode->next = &newEntryNode;
}
printf("should still be word1 %s\n", symbols->first->row->symbol);
return symbols;
}
/* returns the result of adding a use of the given table in the line
with the given number to the table */
//struct table * addUse (char * symbol, int lineNum, struct table * symbols) {
// return symbols;
//}
/* print the table, in the format described above, to the given file */
void printTable (struct table * symbols, FILE * out) {
struct entryNode *n = symbols->first;
printf("%s\t\n", n->row->symbol);
printf("%d\t\n", n->row->linedef);
printf("here it is\n");
}
tabletest.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "table.h"
main() {
struct table *p = newTable();
p = addDef("word1", 1, p);
p = addDef("word2", 2, p);
printTable(p, "out.txt");
return 0;
}
This is the output I get on eclipse:
adding word1
entrycount is 0
should be word1 word1
should still be word1 word1
adding word2
entrycount is 1
should be word1 word1
again should be word1 word2
2should be word1 word2
should still be word1 word2
0Ì"
2280496
here it is
Why is it printing out a weird symbol and letter? And if I'm using malloc how did the contents change to word2 when I was creating new nodes and not assigning anything to it?
Thanks in advance.