I'm trying to print a 2D array of structs using printf in a box formation. Each struct holds an int and char value. When I try to print them it prints the int value just fine, but instead of printing the char value it prints �. When I remove the printf statement telling them to go to a new line after the second for loop(printf("\n")) finishes it prints just fine(except for the fact that it's not in a box formation. Is this some weird quark of C or am I overlooking something?
for(int i = 0; i < States-1; i++){
for(int j = 0; j < 11; j++){
printf("%d%c ", mArray[i][j].state, mArray[i][j].action);
}
printf("\n");
}
The output looks like this:
99� 99� 99� 99� 99� 99� 99� 99� 99� 99� 99�
99� 99� 99� 99� 99� 99� 99� 99� 99� 99� 99�
99� 99� 99� 99� 99� 99� 99� 99� 99� 99� 99�
99� 99� 99� 99� 99� 99� 99� 99� 99� 99� 99�
Here is the rest of my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "classes.h"
void corUsage(argc)
{
if(argc == 1)
{
printf("usage: ./tokenize tm_file\n");
exit(1);
}
}
int curState = 0;
char curAction = 0;
void maketok(char *str)
{
char *ptr = str;
ptr = strtok(str, "/");
curState = atoi(ptr);
ptr = strtok(NULL, " ");
curAction = ptr;
// printf("%d / %s",curState, curAction);
}
struct Matrix
{
{
int state;
char action;
};
int main(int argc, char *argv[])
{
corUsage(argc);
//opens the file given(argc[1])
FILE *fp;
fp = fopen(argv[1], "r");
//checks for errors
if(fp == NULL)
{//error occured
perror(argv[1]);
exit(1);
}
char buffer[256];//a buffer that holds stuff
char *ptr = buffer;//a pointer to the buffer
char *ptr2;//a pointer for MISC.
int States;//The number of states
int Start;//The starting state
int Accept;//The ending state
//this reads the first three lines of the tm.x file and stores
//it in the variable above.
ptr = fgets(buffer, 256, fp);
ptr2 = strtok(ptr, " ");
ptr2 = strtok(NULL, " ");
States = atoi(ptr2);
ptr = fgets(buffer, 256, fp);
ptr2 = strtok(ptr, " ");
ptr2 = strtok(NULL, " ");
Start = atoi(ptr2);
ptr = fgets(buffer, 256, fp);
ptr2 = strtok(ptr, " ");
ptr2 = strtok(NULL, " ");
Accept = atoi(ptr2);
struct Matrix mArray[States][12];//a 2D array that will be holding structs
while((ptr = fgets(buffer, 256, fp)) != NULL)
{
char *cur = strtok(ptr, " ");
int idx = atoi(cur);
char *buffer2[256];//a buffer for MISC data
int co = 0;//counter vairable that tells how many things are in the line.
for(cur = strtok(NULL, " "); cur != NULL; cur = strtok(NULL, " "))
{
buffer2[co] = cur;//possibily not co as index?
co++;
}
for(int q = 0; q < 12; q++)
{
struct Matrix var;
var.state = 99;
var.action = "d";
mArray[idx][q] = var;
}
/*for(int i = 0; i < co; i++)
{
maketok(buffer2[i]);
printf("%d / %s", curState, curAction);
mArray[idx][i].state = curState;
mArray[idx][i].action = curAction;
}*/
for(int i = 0; i < States-1; i++){
for(int j = 0; j < 11; j++){
printf("%d%c ", mArray[i][j].state, mArray[i][j].action);
}
printf("\n");
}
}
File that I'm reading in:
states 10
start 0
accept 9
0 0/0d 1/0d 2/1s 3/3s 4/2s 5/2s 6/5s 7/4s 8/4s 10/9d
1 0/9d 1/9d 2/1s 3/1s 4/1s 5/1s 10/9d
2 0/9d 1/9d 3/2s 4/2s 5/2s 10/9d
3 0/9d 1/9d 3/3s 4/3s 5/9d 10/9d
4 0/9d 1/9d 10/9d
5 0/9d 1/9d 7/6s 10/9d
6 0/6s 1/6s 2/6s 3/6s 4/6s 5/6s 6/6s 7/7s 8/6s 9/6s 10/9d
7 0/6s 1/6s 2/6s 3/6s 4/6s 5/6s 6/8s 7/7s 8/6s 9/6s 10/9d
8 0/9d 1/9d 10/9d
printf()
(presumably) with agets()
and a constant string argument seems more likely to cause trouble than cure it. Your question would be more convincing if the code were compilable:struct Matrix { { int state; …
is not valid C. – Jonathan Leffler