如何通过调用如下函数来打印字符串数组:

void function(char**string);    //sample1
void function(char string[][LEN2], int size);    //sample2
void function(char (*string)[LEN2], int size);    //sample3


我认为2和3是正确的;

此问题的正确格式并不重要。

我想知道,计算机如何理解sample1(尤其是在内存中)而不仅仅是记录正确的答案。

谢谢(我第一次使用,可能有点荒谬。)

使用Visual Studio 2017,关闭安全检查。在PC上运行。

#include<stdio.h>
#define LEN1 10
#define LEN2 100
void item1(char**string);
void print_initial_string(char**string);

int main(void)
{
    char string[LEN1][LEN2] = {"a", "ab", "abc", "abcd", "abcde",
            "c", "cd", "cde", "cdgh", "seids"};
    item1(string);
}

/*implements of functions*/
void item1(char**string)
{
    print_initial_string(string);
}
void print_initial_string(char**string)
{
    char (*c)[LEN2] = string[0];
    for (int i = 0; i < LEN1; i++)
        puts(c);     /*-- stopped at here --*/
}


我认为它将打印字符串,但是失败了。

并返回代码-1073741819

最佳答案

如果我编译时执行gcc -pedantic -Wextra ar.c,则会收到很多指示问题的消息:

pi@raspberrypi:~/Downloads $ gcc -pedantic -Wextra ar.c
ar.c: In function ‘main’:
ar.c:11:11: warning: passing argument 1 of ‘item1’ from incompatible pointer type [-Wincompatible-pointer-types]
     item1(string);
           ^~~~~~
ar.c:4:6: note: expected ‘char **’ but argument is of type ‘char (*)[100]’
 void item1(char**string);
      ^~~~~
ar.c: In function ‘print_initial_string’:
ar.c:21:23: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
     char (*c)[LEN2] = string[0];
                       ^~~~~~
ar.c:23:14: warning: passing argument 1 of ‘puts’ from incompatible pointer type [-Wincompatible-pointer-types]
         puts(c);     /*-- stopped at here --*/
              ^
In file included from ar.c:1:0:
/usr/include/stdio.h:697:12: note: expected ‘const char *’ but argument is of type ‘char (*)[100]’
 extern int puts (const char *__s);
            ^~~~




char**stringchar*的数组,这意味着字符串中的每个条目都是一个char*(因此是一个指针),这不是char string [LEN1] [LEN2]的原因,因为其中不包含指针

所以void item1(char (*string)[LEN2])void print_initial_string(char (*string)[LEN2])

char (*c)[LEN2] = string[0];也不行,c是char *,但是您说它是指向char[LEN2]的指针,您需要char (*c)[LEN2] = &string[0];或仅char (*c)[LEN2] = string;。在这种情况下puts(c)必须是puts(c[i]);因为C不是字符串而是一个指针

最后:

#include<stdio.h>

#define LEN1 10
#define LEN2 100

void item1(char (*string)[LEN2]);
void print_initial_string(char (*string)[LEN2]);

int main(void)
{
    char string[LEN1][LEN2] = {"a", "ab", "abc", "abcd", "abcde",
                               "c", "cd", "cde", "cdgh", "seids"};
    item1(string);
}

/*implements of functions*/
void item1(char (*string)[LEN2])
{
    print_initial_string(string);
}
void print_initial_string(char (*string)[LEN2])
{
  char (*c)[LEN2] = string;

  for (int i = 0; i < LEN1; i++)
    puts(c[i]);
}


编译与执行:

pi@raspberrypi:~/Downloads $ gcc -g -pedantic -Wextra ar.c
pi@raspberrypi:~/Downloads $ ./a.out
a
ab
abc
abcd
abcde
c
cd
cde
cdgh
seids


在valgrind下执行:

pi@raspberrypi:~/Downloads $ valgrind ./a.out
==11987== Memcheck, a memory error detector
==11987== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==11987== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==11987== Command: ./a.out
==11987==
a
ab
abc
abcd
abcde
c
cd
cde
cdgh
seids
==11987==
==11987== HEAP SUMMARY:
==11987==     in use at exit: 0 bytes in 0 blocks
==11987==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==11987==
==11987== All heap blocks were freed -- no leaks are possible
==11987==
==11987== For counts of detected and suppressed errors, rerun with: -v
==11987== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)




char**是正确的示例:

#include <stdio.h>
#include <stdlib.h>

#define LEN 10

void pr(const char ** a, int sz)
{
  for (int i = 0; i != sz; ++i)
    puts(a[i]);
}

int main()
{
  const char *a[LEN] = { "a", "ab", "abc", "abcd", "abcde",
                         "c", "cd", "cde", "cdgh", "seids"};

  pr(a, LEN);
}


我都使用const,因为文字字符串常量是

编译与执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wall aa.c
pi@raspberrypi:/tmp $ ./a.out
a
ab
abc
abcd
abcde
c
cd
cde
cdgh
seids

关于c - char [] []和char **之间的区别,为什么puts(strings [0])无法打印(strings定义为char ** strings),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54724497/

10-13 08:10
查看更多