Angelacolin
Angelacolin
采纳率0%
2014-06-01 23:09

C语言中这个指针定义是什么意思?

const char *const ch
为什么要2个const?分别是什么意思?顺便求详细说一下这个指针具体是什么意思?

编辑下,完整程序如下:

#include
#include
#include
#include

int 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 -%s\n", *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 = '\0';
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;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • u013295579 诸峰之巅 7年前

    把你的完整程序发给我吧,我办你看一下。

    点赞 评论 复制链接分享
  • u013295579 诸峰之巅 7年前

    第一个const是用来定义常量,而把ch声明成const,是为了确保ch指向的串不改变。一般情况下,标准库函数不需要修改调用变元指向的对象时,对应参数都声明成const型,确保在程序中调用时无误。

    点赞 评论 复制链接分享
  • lym753024200 风浪静 7年前

    楼主经常逃避结贴的话那就没人来回答了哟

    点赞 评论 复制链接分享

相关推荐