问题描述
- C语言中这个指针定义是什么意思?
-
const char *const ch
为什么要2个const?分别是什么意思?顺便求详细说一下这个指针具体是什么意思?编辑下,完整程序如下:
#include
#include
#include
#includeint find_char(const int, const char *const, const int);
int main(int argc, char argv) {
bool translate = false, squeeze = false, delete = false;
char *chars1, *chars2;
if (++argv == '-') {
if (*++*argv == 's')
squeeze = true;
else if (**argv == 'd')
delete = true;
else {
printf("Unknown option -%sn", *argv);
return EXIT_FAILURE;
}
++argv;
}
if (delete) {
if (argc != 3) {
printf("-d should be followed by exactly one string.n");
return EXIT_FAILURE;
}
}
else if (squeeze) {
if (argc == 4)
translate = true;
else if (argc != 3) {
printf("-s should be followed by one string or two.n");
return EXIT_FAILURE;
}
}
else if (argc != 3) {
printf("Without any option,"
" exactly two strings should be given as arguments.n");
return EXIT_FAILURE;
}
else
translate = true;
chars1 = *argv;
if (translate && strlen(chars1) != strlen(chars2 = *++argv)) {
printf("The strings provided as arguments should have equal lengths.n");
return EXIT_FAILURE;
}
int current, previous = '';
int nb_of_chars = strlen(chars1);
while ((current = getchar()) != EOF) {
if (delete) {
if (find_char(current, chars1, nb_of_chars) == nb_of_chars)
putchar(current);
continue;
}
int i = find_char(current, chars1, nb_of_chars);
if (translate) {
if (i != nb_of_chars)
current = chars2[i];
if (squeeze && (i = find_char(current, chars2, nb_of_chars)) != nb_of_chars)
current = chars2[i];
}
if (squeeze && i != nb_of_chars && current == previous)
continue;
putchar(current);
previous = current;
}
return EXIT_SUCCESS;
}int find_char(int current, const char *const chars, const int nb_of_chars) {
for (int i = 0; i < nb_of_chars; ++i) {
if (current == chars[i])
return i;
}
return nb_of_chars;
}
解决方案
楼主经常逃避结贴的话那就没人来回答了哟
解决方案二:
第一个const是用来定义常量,而把ch声明成const,是为了确保ch指向的串不改变。一般情况下,标准库函数不需要修改调用变元指向的对象时,对应参数都声明成const型,确保在程序中调用时无误。
解决方案三:
把你的完整程序发给我吧,我办你看一下。