简体   繁体   English

分割错误,阵列打印C

[英]Segmentation Fault, Array Printing C

I am trying to write a program that takes user-input strings and prints them out as a 7 by 5 asterisk grid. 我正在尝试编写一个程序,该程序接受用户输入的字符串并将其打印为7 x 5星号网格。 I am currently trying to figure out how to print put the letters as I need to print them line by line in order for the different letters to be printed side by side. 我目前正在试图弄清楚如何打印字母,因为我需要逐行打印它们,以便并排打印不同的字母。

Anyway, my attempt so far is this: 无论如何,到目前为止我的尝试是:

#include <stdio.h>
#include <string.h>


char capitalA[7][5] = { 
"  *  ",
" * * ",
"*   *",
"*****",
"*   *",
"*   *",
"*   *"
};
int i;
int j;

int main() 
{//main

    for (i = 0; i < 5 ; i++) 
    {
        for (j = 0; j < 7 ; j++) 
        {
            printf("%s\n", capitalA[j][i]);
        }
    }
    return (0);
}//*main

My desired output is the asterisk A, but I get a Segmentation fault. 我想要的输出是星号A,但是出现了分段错误。

char capitalA[7][5]

is not large enough. 还不够大。 The strings literals you use in the initialization automatically include the \\0 character, so you need to use 您在初始化中使用的字符串文字会自动包含\\0字符,因此您需要使用

char capitalA[7][6] 

instead. 代替。 And change your loops to a single one 并将循环更改为一个循环

for (j = 0; j < 7 ; j++)
    printf("%s\n", capitalA[j]);

since right now you are displaying char s using %s specifier. 从现在开始,您正在使用%s说明符显示char

If you're not happy with the '\\0' being added automatically and need to save space, you can use the code you currently have or you need to initialize the array using brace initialization like 如果您对自动添加'\\0'感到不满意并且需要节省空间,则可以使用当前使用的代码,或者需要使用括号初始化来初始化数组,例如

char capitalA[7][5] = {{' ', ' ', '*', ' ', ' '}, ...};

and stick to displaying the characters char by char (making sure that you use the '%c' specifier instead of the '%s' ). 并坚持按char显示字符char (确保您使用'%c'说明符而不是'%s' )。

This: 这个:

for (i = 0; i < 5 ; i++) {
    for (j = 0; j < 7 ; j++) {
        printf("%s\n", capitalA[j][i]);
}
}

should be 应该

for (i = 0; i < 7; i++) {
    for (j = 0; j < 5; j++) {
        printf("%c", capitalA[i][j]);
    }
    printf("\n");
}

because 因为

  1. The correct format specifier for a char is %c , not %s . char的正确格式说明符是%c ,而不是%s
  2. You mixed up the loops. 您混淆了循环。 The outer one should loop 7 times while the inner one should loop 5 times. 外层应循环7次,内层应循环5次。
  3. You should print a \\n after the inner loop has finished executing and not in each iteration of the inner loop. 您应该打印\\n内部循环完成执行,而不是在内部循环的每次迭代之后
printf("%s\n", capitalA[j][i]);

should be 应该

printf("%c", capitalA[j][i]);

What you need is to print character by character but you are trying to print a string using %s which tries to print a string until \\0 is encountered. 您需要按字符打印字符,但是您尝试使用%s来打印字符串,这会尝试打印字符串直到遇到\\0

Since %s is trying to access array out of bound you see a segmenation fault. 由于%s试图越界访问阵列,您会看到分段错误。

If you want to go with printing char by char then make sure you insert \\n after each row 如果要逐字符打印字符,请确保在每行之后插入\\n

 for (i = 0; i < 5 ; i++) 
 {
    for (j = 0; j < 7 ; j++) 
    {
        printf("%s\n", capitalA[j][i]);
    }
 }

You are printing char type array. 您正在打印char类型数组。 Not a single character. 没有一个字符。 Change it to 更改为

for (i = 0; i < 5 ; i++) 
{
    for (j = 0; j < 7 ; j++) 
    {
        printf("%c", capitalA[j][i]);
    }
    printf("\n");
}

just use one for loop, and change 5 to 6 in the array declaration like this : 只需使用一个for循环,然后在数组声明中将5更改为6即可:

char capitalA[7][6]
//  and
for(j=0 ; j<7 ; j++)
    printf("%s\n",capitalA[j]);

your program will be like this after modification, try it ^^: 修改后您的程序将如下所示,尝试一下^^:

#include <stdio.h>
#include <string.h>

//edit the column nomber to support the '\0' it will be 6 not 5.
char capitalA[7][6] = { 
"  *  ",
" * * ",
"*   *",
"*****",
"*   *",
"*   *",
"*   *"
};
int i;
int j;

int main() 
{//main


        for (j = 0; j < 7 ; j++) 
        {
            printf("%s\n", capitalA[j]);
        }

    return (0);
}//*main

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

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