gpt4 book ai didi

c - char[][]和char**的区别,为什么puts(strings[0])不能打印(strings被定义为char**strings)

转载 作者:行者123 更新时间:2023-11-30 14:41:51 25 4
gpt4 key购买 nike

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

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* 的数组,这意味着 string 中的每个条目都是 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,因为文字字符串是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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com