Если я компилирую, делая 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**string
- это массив char*
, это означает, что каждая запись в string - это char*
(так что указатель), это не то, что символьная строка [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