简体   繁体   English

在c中打印出一个二维字符数组

[英]Printing out a 2d array of char in c

I want to print out a 2d array of char that contains mips registers.我想打印出一个包含 mips 寄存器的二维字符数组。 I want to print each register separately.我想分别打印每个寄存器。 But my current code prints out all the registers together.但是我当前的代码将所有寄存器一起打印出来。 Is there a better way to do this problem, such as using a double pointer array of chars?有没有更好的方法来解决这个问题,比如使用字符的双指针数组?

My code so far:
char reg[][3] = {
   {'$', 's', '0'},
   {'$', 's', '1'},
   {'$', 's', '2'},
   {'$', 's', '3'},
   {'$', 's', '4'},
   {'$', 's', '5'},
   {'$', 's', '6'},
};

int main(void){

   for(int i = 0; i < sizeof(reg); i++)
   {
      printf("%s\n", reg[i]);
   }
   return 0;
}
My output:
$s0$s1$s2$s3$s4$s5$s6
$s1$s2$s3$s4$s5$s6
$s2$s3$s4$s5$s6
$s3$s4$s5$s6
$s4$s5$s6
$s5$s6
$s6
But I want my output to be like this:
$s0
$s1
$s2
$s3
$s4
$s5
$s6

This is happening because in C, strings are null-terminated by convention.发生这种情况是因为在 C 中,字符串按照惯例以空字符结尾 This means that printf called with the %s specifier will not stop until it reaches a null character .这意味着使用 %s 说明符调用的 printf 在到达空字符之前不会停止

To get your desired output without changing much of your code, you could add a null character at the end of each register like this为了在不更改大部分代码的情况下获得所需的输出,您可以像这样在每个寄存器的末尾添加一个空字符

char reg[][4] = {
   {'$', 's', '0', 0},
   {'$', 's', '1', 0},
   {'$', 's', '2', 0},
   {'$', 's', '3', 0},
   {'$', 's', '4', 0},
   {'$', 's', '5', 0},
   {'$', 's', '6', 0}
};

but that's not very pretty.但这不是很漂亮。 You can write it like this:你可以这样写:

char reg[][4] = {"$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6"};

These strings will be null terminated by default.默认情况下,这些字符串将以空字符结尾。

Another way to do this without changing your array will be to specify how many characters to print for each print call, something like this:在不更改数组的情况下执行此操作的另一种方法是指定每次打印调用要打印的字符数,如下所示:

printf("%.3s\n", reg[i]);

Edit: As pointed out by ice05's answer, your loop condition is also false.编辑:正如 ice05 的回答所指出的,您的循环条件也是错误的。 This code might generate a segmentation fault.此代码可能会生成分段错误。 A common way to handle this case in C is to use a preprocessor macro, for example:在 C 中处理这种情况的常用方法是使用预处理器宏,例如:

#define REGISTER_STRING_SIZE 3

char reg[][REGISTER_STRING_SIZE] = {
   {'$', 's', '0'},
   {'$', 's', '1'},
   {'$', 's', '2'},
   {'$', 's', '3'},
   {'$', 's', '4'},
   {'$', 's', '5'},
   {'$', 's', '6'},
};

And then in your loop,然后在你的循环中,

    for(int i = 0; i < sizeof(reg)/REGISTER_STRING_SIZE; i++)
    {
        printf("%.3s\n", reg[i]);
    }

This is not very elegant and I would recommend treating the registers as strings (adding the null terminator).这不是很优雅,我建议将寄存器视为字符串(添加空终止符)。

First of all, it may be easier to treat the register names as strings, (ie, char arrays terminated by a \\0 ), especially since you are using %s to print out the string (the %s specifier expects a NUL terminated array of chars).首先,将寄存器名称视为字符串可能更容易(即,以\\0结尾的字符数组),特别是因为您使用%s打印出字符串( %s说明符期望以 NUL 结尾的数组字符)。 So, you want the second dimension of the 2D reg array to be 4, in order to hold the \\0 :因此,您希望 2D reg数组的第二维为 4,以保持\\0

char reg[][4] = {
   {'$', 's', '0', '\0'},
   {'$', 's', '1', '\0'},
   {'$', 's', '2', '\0'},
   {'$', 's', '3', '\0'},
   {'$', 's', '4', '\0'},
   {'$', 's', '5', '\0'},
   {'$', 's', '6', '\0'}
};

Alternatively, you could write these out as strings directly:或者,您可以直接将这些写为字符串:

char reg[][4] = {
   "$s0",
   "$s1",
   "$s2",
   "$s3",
   "$s4",
   "$s5",
   "$s6"
};

Now onto the loop.现在进入循环。 The sizeof(reg) in the loop condition gives you the size (in bytes) of the 2D array reg which is 7*4 = 28.循环条件中的sizeof(reg)为您提供 7*4 = 28 的二维数组reg的大小(以字节为单位)。

What you really want to loop over is the number of rows in the 2D array.您真正想要循环的是二维数组中的行数。 In your case, since the size of each register name is 4 (remember to include the NUL terminator!), you could do the following:在您的情况下,由于每个寄存器名称的大小为 4(请记住包括 NUL 终止符!),您可以执行以下操作:

for(int i = 0; i < sizeof(reg)/4; i++)
{
    printf("%s\n", reg[i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM