[英]printing 2D character array
I keep getting a nest functions are forbidden error. 我不断收到嵌套函数被禁止的错误。 When I try to use a pointer as well I receive and error reporting that "initialization makes pointer from integer without a cast."
当我也尝试使用指针时,我收到并报告错误:“初始化使指针从整数开始而没有强制转换。” and unsure what's going on with that.
并不确定发生了什么。 (using -ansi and -pedantic )
(使用-ansi和-pedantic)
#include <stdio.h>
#include <stdlib.h>
void createArt(int m) {
int i = 0, j =0, k = 1, l = 1;
char ascii_art[5][5] {
{'/','/','/','/','/'},
{'/','/','/','/','/'},
{'/','/','/','/','/'},
{'/','/','/','/','/'},
{'/','/','/','/','/'}
};
for(i; i < (5 * m); i++) {
for(j; j < (5 * m); j++) {
printf("%s", ascii_art[i][j]); /* can't print out chars */
}
}
}
int main() {
int multiplier = 0, m = 1;
printf("Enter a number: ");
scanf("%d", &multiplier);
createArt(multiplier);
return 0;
}
There should be a =
after char ascii_art[5][5]
and char
s are printed with %c
, not %s
. char ascii_art[5][5]
和char
s用%c
而不是%s
打印后应该有一个=
。
I cannot reproduce the initialization makes pointer from integer without a cast
error, but you have several unused variables and the first i
in for(i; i < (5 * m); i++) {
has no effect. 我无法
initialization makes pointer from integer without a cast
重现initialization makes pointer from integer without a cast
错误,但是您有几个未使用的变量,并且for(i; i < (5 * m); i++) {
的第一个i
无效。 You can leave it out as well. 您也可以忽略它。 (Same for the other loop).
(与其他循环相同)。
At least in the second loop (first one wouldn't hurt either) you probably want to write j=0
, otherwise the inner loop will only be executed for i=0
. 至少在第二个循环中(第一个也不会),您可能想要写
j=0
,否则内部循环将仅在i=0
执行。
Your code will also fail if multiplier
is greater than 1
, because then i
and j
will become greater than 5
and you will try to access the array out-of-bounds. 如果
multiplier
大于1
,您的代码也会失败,因为i
和j
会变得大于5
并且您将尝试访问数组越界。
Issues that i found so far 我到目前为止发现的问题
1)You missing =
after declaration ascii_art 1)您缺少
=
声明ascii_art后
2) You should replace %s with %c in 2)您应将%s替换为%c
printf("%s", ascii_art[i][j]);
%s
is for printing string terminated with \\0
, and for single character you need to use %c
%s
用于打印以\\0
结尾的字符串,对于单个字符,您需要使用%c
Also i'm afraid you going to do something bad with that multiplier since your loop stopping condition is i< 5*m
and m obtained from user with scanf. 另外,由于循环停止条件是
i< 5*m
并且使用scanf从用户获得i< 5*m
,因此恐怕您会对该乘数做不好的事情。 I see only two valid options for multiplier in this case 0, 1 any other value will take out of your array. 在这种情况下,我只看到两个有效的乘法器选项,0、1以及其他任何值将从数组中取出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.