I'm trying to code my own Turing machine. My program takes two files as arguments: my initial tape the machine will have to work with, and a rule file. This rule file consists in one rule per line, and each rule is five integers: current state, symbol found, new symbol, direction for the head to go, and new state. For that, I have a function that reads my file and puts each set of five ints found in a rule structure. An array of these structures is then generated.
What I'm trying to do is to return this array in order to be able to use it later on. Here is what I have :
struct rule {
int cur_state;
int symbol;
int new_symbol;
int direction;
int new_state;
};
typedef struct rule rule;
struct vect {
int nb_elem;
rule *p;
};
vect rule_generator (char * file_rule){
int line_number = 0;
int ligne;
char tmp;
rule rule_list[line_number];
vect output_rules;
FILE * file;
file = fopen(file_rule, "r");
for(tmp = getc(file); tmp != EOF; tmp = getc(file)){
if ( tmp == '\n')
line_number++;
}
output_rules.p = malloc(line_number*sizeof(rule));
assert(output_rules.p);
output_rules.nb_elem = line_number;
for (ligne = 0; ligne < line_number; ligne++ ){
fscanf(file, "%d %d %d %d %d", &rule_list[ligne].cur_state,
&rule_list[ligne].symbol, &rule_list[ligne].new_symbol,
&rule_list[ligne].direction, &rule_list[ligne].new_state);
}
fclose(file);
return output_rules;
}
int main (int argc, char *argv[]){
vect rule_list = rule_generator(argv[2]);
printf("symbole : %ls \n", &rule_list.p[0].symbol);
return 0;
}
As some of you may already have guessed, this doesn't print anything... I've been scratching my head for a while, trying to access my array. I could really use a hand here!
Few problems here.
You are declaring array with size 0.
int line_number = 0;
rule rule_list[line_number];
You don't need rule_list just remove it.
output_rules is no where being initilized other than memory allocating.
Solution:
I would suggest you to use output_rules for fscanf.
for (ligne = 0; ligne < line_number; ligne++ ){
fscanf(file, "%d %d %d %d %d", &output_rules.p[ligne].cur_state,
&output_rules.p[ligne].symbol, &output_rules.p[ligne].new_symbol,
&output_rules.p[ligne].direction, &output_rules.p[ligne].new_state);
}
Also did you mean to print the value of symbol?
As you are using %ls which is used to wchar_t *.
printf("symbole : %ls \n", &rule_list.p[0].symbol);
should be
printf("symbole : %d \n", rule_list.p[0].symbol);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With