S1E28动动手第1题
本帖最后由 helloooo 于 2022-12-9 03:09 编辑可以编译通过,但是提示warning: initialization of 'char (**)' from incompatible pointer type 'char **'
应该怎么定义p才没问题?
#include <stdio.h>
#define HEIGHT 7
void print_FISHC(int gap);
int main()
{
int gap;
printf("please input the number of gap: ");
scanf("%d", &gap);
print_FISHC(gap);
return 0;
}
void print_FISHC(int gap)
{
char *f = {
"########",
"## ",
"## ",
"######",
"## ",
"## ",
"## "};
char *i = {
"####",
" ## ",
" ## ",
" ## ",
" ## ",
" ## ",
"####"};
char *s = {
" ###### ",
"## ##",
"## ",
" ###### ",
" ##",
"## ##",
" ###### "};
char *h = {
"## ##",
"## ##",
"## ##",
"########",
"## ##",
"## ##",
"## ##"};
char *c = {
" ###### ",
"## ##",
"## ",
"## ",
"## ",
"## ##",
" ###### "};
char (**p) = {f, i, s, h, c};
int n, m, k;
for (n = 0; n < HEIGHT; ++n)
{
for (m = 0; m < 5; ++m)
{
printf("%s", *p);
for (k = 0; k < gap; k++)
printf(" ");
}
printf("\n");
}
} 本帖最后由 jhq999 于 2022-12-9 07:35 编辑
char **p = {f, i, s, h, c};///////////////////////char * 类型是字符串指针数组,p就应该是字符串指针数组的数组
int n, m, k;
for (n = 0; n < HEIGHT; ++n)
{
for (m = 0; m < 5; ++m)
{
printf("%s", p);//////////////////////////////////////p=f,p=f= "########",
for (k = 0; k < gap; k++)
printf(" ");
}
printf("\n");
} jhq999 发表于 2022-12-9 07:30
#include <stdio.h>
#define HEIGHT 7
void print_FISHC(int gap);
int main()
{
int gap;
printf("please input the number of gap: ");
scanf("%d", &gap);
print_FISHC(gap);
return 0;
}
void print_FISHC(int gap)
{
char *f = {
"########",
"## ",
"## ",
"######",
"## ",
"## ",
"## "};
char *i = {
"####",
" ## ",
" ## ",
" ## ",
" ## ",
" ## ",
"####"};
char *s = {
" ###### ",
"## ##",
"## ",
" ###### ",
" ##",
"## ##",
" ###### "};
char *h = {
"## ##",
"## ##",
"## ##",
"########",
"## ##",
"## ##",
"## ##"};
char *c = {
" ###### ",
"## ##",
"## ",
"## ",
"## ",
"## ##",
" ###### "};
char **p = {f, i, s, h, c};
int n, m, k;
for (n = 0; n < HEIGHT; ++n)
{
for (m = 0; m < 5; ++m)
{
printf("%s", *p);
for (k = 0; k < gap; k++)
printf(" ");
}
printf("\n");
}
}
卡,报错了,报的是段错误 zhangjinxuan 发表于 2022-12-9 08:01
卡,报错了,报的是段错误
我这里没报错 zhangjinxuan 发表于 2022-12-9 08:01
卡,报错了,报的是段错误
printf("%s", *p); jhq999 发表于 2022-12-9 10:32
我这里没报错
编译器不一样吧 zhangjinxuan 发表于 2022-12-9 10:39
编译器不一样吧
看看5楼我的回复你多了*号 jhq999 发表于 2022-12-9 07:30
我大概懂了
char **p = {f, i, s, h, c}; //p是一个指针数组,数组元素是指向字符指针的指针。比如p = f = &f,就是一个指向字符指针的指针
int n, m, k;
for (n = 0; n < HEIGHT; ++n)
{
for (m = 0; m < 5; ++m)
{
printf("%s", p);
for (k = 0; k < gap; k++)
printf(" ");
}
printf("\n");
}
或者这样
char *(*p) = {&f, &i, &s, &h, &c}; //p是一个指针数组,数组元素是指向字符指针数组的指针,解引用的时候要加一层
int n, m, k;
for (n = 0; n < HEIGHT; ++n)
{
for (m = 0; m < 5; ++m)
{
printf("%s", (*p));
for (k = 0; k < gap; k++)
printf(" ");
}
printf("\n");
}
页:
[1]