【问题标题】:Issue with printing 2D array of structs with "\n"使用“\n”打印二维结构数组时出现问题
【发布时间】:2014-12-06 05:29:46
【问题描述】:

我正在尝试使用 printf 以盒子形式打印结构的二维数组。每个结构都包含一个 int 和 char 值。当我尝试打印它们时,它会很好地打印 int 值,但它不会打印 char 值,而是打印�。当我删除 printf 语句告诉他们在第二个 for loop(printf("\n")) 完成后转到新行时,它打印得很好(除了它不是盒子形式的事实。这有点奇怪吗C 的夸克还是我忽略了什么?

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");
}

输出如下:

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�  

这是我的其余代码:

#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");
            }

}

我正在阅读的文件:

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

【问题讨论】:

  • 请发布您的所有代码。
  • 请发布完整的代码以及您谈到的打印循环的变体。
  • 您正在阅读的文件中有什么内容?
  • 所以我发现如果我将 print("\n") 替换为 gets("") 可以解决问题,但我仍然不知道为什么会这样。
  • 用gets() 和常量字符串参数替换printf()(大概)似乎更容易造成麻烦而不是解决问题。如果代码是可编译的,你的问题会更有说服力:struct Matrix { { int state; … is not valid C.

标签: c arrays for-loop struct printf


【解决方案1】:

好的,所以我想通了。

在这个循环中:

for(int q = 0; q < 12; q++)
        {
                struct Matrix var;
                var.state = 99;
                var.action = "d";
                mArray[idx][q] = var;
        }

我声明var.action = "d";。 它应该是var.action = 'd';,只用单引号。

感谢任何花时间查看此内容的人。

【讨论】:

  • 这很可能会在编译时给您一个警告,因为您将char* 分配给char。如果您仔细查看警告,您将不太容易出现此类问题。
  • 如果编译器没有警告您该分配,您需要打开编译警告,或获得更好的编译器。请记住,当您学习 C 时(甚至可能更长时间),编译器比您更了解 C。如果它发出警告,则意味着您的程序存在问题,您应该注意它的警告并修复您的代码。当您知道足以忽略编译器警告时,您就会知道——而且不会在您需要问这样的问题时。 (我经常用gcc -g -O3 -std=c11 -Wall -Wextra -Werror 加上一些额外的东西来编译。)
猜你喜欢
  • 1970-01-01
  • 2015-03-15
  • 2017-04-11
  • 1970-01-01
  • 2015-02-03
  • 2020-06-25
  • 2020-02-07
  • 2021-01-22
相关资源
最近更新 更多