[英]C - Printing a 2D Char Array
How do I print the elements of a 2D Char Array in C? 如何在C中打印2D字符数组的元素?
Here is my current code: 这是我当前的代码:
int main()
{
unsigned int size;
printf("Enter size:\n");
scanf("%d",&size);
char word[size][size];
//Enter the matrix
for(int k = 0; k < (size); ++k){
for (int j = 0; j < (size); ++j){
printf("Enter letter:");
scanf("%c",&word[k][j]);
}
}
//printf("\n");
for (int k = 0; k < size; ++k){
for(int j = 0; j < size; ++j){
printf("%c",word[k][j]);
}
//printf("\n ");
}
printf("\n");
}
When executed it returns the element in pairs (using a 4x4 array) Example: 执行后,它将成对返回元素(使用4x4数组)。示例:
ab
cd
ef
gh
ij
kl
mn
op
Rather than my desired output: 而不是我想要的输出:
abcd
efgh
ijkl
mnop
Why is this? 为什么是这样?
changing your scanf solves all the problems 更改scanf可解决所有问题
scanf(" %c",&word[k][j]); // notice the space before '%c'
And also you need to change your printing loop to this 而且您还需要将打印循环更改为此
for (k = 0; k < size; ++k){
for(j = 0; j < size; ++j){
printf("%c",word[k][j]);
}
printf("\n");
}
I removed the reading and it seems like printing is ok: 我删除了阅读内容,看来可以打印了:
int main()
{
const unsigned int size = 4;
char word[size][size];
//Enter the matrix
for (int k = 0; k < (size); ++k) {
for (int j = 0; j < (size); ++j) {
word[k][j] = 'a' + j + (k * size);
}
}
for (int k = 0; k < size; ++k) {
for (int j = 0; j < size; ++j) {
printf("%c", word[k][j]);
}
printf("\n");
}
printf("\n");
getchar();
return 0;
}
And the output: 并输出:
abcd
efgh
ijkl
mnop
I found two issues with your source. 我发现您的来源有两个问题。
One is the memory allocation - that is actually not ansi-c. 一种是内存分配-实际上不是ansi-c。
If you need dynamic memory you need to allocate it at runtime. 如果需要动态内存,则需要在运行时分配它。 Consider switching to c++ since there are standard facilities that help with that in a safer way. 考虑使用c ++,因为有一些标准的工具可以更安全地为您提供帮助。
The second issue was that there is a whitespace character in the buffer that is used as an input character. 第二个问题是缓冲区中有一个空格字符用作输入字符。 I think you want to clear that. 我想你想澄清一下。
Here is the source with additional comments: 这是带有其他注释的来源:
#include <stdio.h>
#include <stdlib.h>
void ansiC()
{
unsigned int size;
printf("Enter size:\n");
scanf("%d", &size);
//char word[size][size]; <- this is not ansi-c because size is unknown at compile time
char * word = (char*)malloc(sizeof(char)* size * size);
//Enter the matrix
for (int k = 0; k < (size); ++k)
{
for (int j = 0; j < (size); ++j)
{
printf("Enter letter:");
scanf("%c ", &word[k * size + j]);
//since word is just a pointer i changed the way the position is calculated
//after the character the user presses the enter key
//this puts a whitespace character on the buffer.
//by adding the space after %c you also clear that from the buffer
}
}
//printf("\n");
for (int k = 0; k < size; ++k)
{
for (int j = 0; j < size; ++j)
{
printf("%c", word[k * size + j]);
//since word is just a pointer i changed the way the position is calculated
}
//printf("\n ");
}
printf("\n");
free(word); //if you use malloc you need to remember to use free
}
int main()
{
ansiC();
return 0;
}
Beware: %c
and %1s
do different things (apart from adding a terminating null for the latter): 注意: %c
和%1s
做不同的事情(除了为后者添加终止null之外):
c
take every character including space, tab, cr and lf c
取每个字符, 包括空格,制表符,cr和lf %1s
skip over all blanks (space, tab, cr, lf, etc.) %1s
跳过了所有空格(空格,制表符,cr,lf等) So at input time, you should use: 因此,在输入时间,您应该使用:
char c[2]; // provide room for a terminating null...
...
for(int k = 0; k < (size); ++k){
for (int j = 0; j < (size); ++j){
printf("Enter letter:");
scanf("%1s",c);
word[k][j] = c[0];
}
}
And at print time: 在打印时:
for (int k = 0; k < size; ++k){
for(int j = 0; j < size; ++j){
printf("%c",word[k][j]);
}
printf("\n "); // new line after each line
}
Please check this . 请检查一下。
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
char arr[5][3]={"abc","aks","tny","dkn","kbf"};
for(int a=0;a<5;a++)
{
for(int b=0;b<3;b++)
{
cout<<" "<<arr[a][b];
}
cout<<endl;
}
getch();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.