c语言中用指针对字符串输出的问题

求解释,下面两输出方法为啥都不能正常输出

#include
#define N 5
#include  
int main()
{
    char ch[N][30],*p;
    int i;
    p=ch[0];
    
    for(i=0;igets(ch[i]);
    for(i=0;iputs(p[i]);
    for(i=0;iputs(ch[i]);
}

img

你都没输入字符串,输出啥啊?

最后一行是可以输出的,问题倒数第二行出错终止了
指针用法有误,看下面修改过的。

img

#include<stdio.h>
#define N 5
#include <string.h> 
int main()
{
    char ch[N][30],(*p)[30];
    int i;
    p=ch[0];
    for(i=0;i<N;i++)gets(ch[i]);
    for(i=0;i<N;i++)puts(p[i]);
    for(i=0;i<N;i++)puts(ch[i]);
}


不同编译器,有的写法会报提醒,你给几个初始值,再打印就好看结果了

#include<stdio.h>
#define N 5
#include <string.h>
int main()
{
    char ch[N][30] = {"hello", "world", "asdasd", "123", "asuida"}, (*p)[30];
    int i;
    p=ch;

    //for(i=0;i<N;i++) gets(ch[i]);                                                                           
    for(i=0;i<N;i++) puts(p[i]);
    for(i=0;i<N;i++) puts(ch[i]);
    return 0;
}

/*
hello
world
asdasd
123
asuida
hello
world
asdasd
123
asuida
*/
  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/719663
  • 你也可以参考下这篇文章:c语言输出字符数组出现汉字乱码解决方法
  • 除此之外, 这篇博客: c语言从入门到精通中的 数组元素也是一种变量, 其标识方法为数组名后跟一个下标。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 组中的顺序号。数组元素的一般形式为:
        数组名[下标]
    其中下标只能为整型常量或整型表达式。如为小数时,C编译将自动取整。例如:
        a[5]
        a[i+j]
        a[i++]
    都是合法的数组元素。
    
    或是使用指针访问地址,这个后面说