c - C 中的 char *s[] 和 char s[][20] 有什么区别?

标签 c string

检查第一个代码,如果我写 char *tracks[] 而不是 char tracks[][80],它似乎仍然可以完美地工作。

#include <stdio.h>
#include <string.h>

char tracks[][80] = { "I left my heart in Harvard Med School",
        "Newark, Newark - a wonderful town", "Dancing with a Dork",
        "From here to maternity", "The girl from Iwo Jima", };

void find_track(char search_for[]) {    //'char *search_for' is equivalent.
    int i;
    for (i = 0; i < 5; i++) {
        if (strstr(tracks[i], search_for))
            printf("Track %i(Line %i):'%s'\n", i, i + 1, tracks[i]);
    }
}

int main() {
    char search_for[80];
    printf("Search for: ");
    scanf("%79s", search_for);  //fgets(search_for,80,stdin);
    find_track(search_for);
    return 0;
}

但是检查第二个代码,如果我尝试使用 char juices[][20] 而不是 char *juices[] ,编译器会给出错误,我想要知道为什么吗?

#include <stdio.h>
#include <string.h>

void print_reverse(char *s) {
    size_t len = strlen(s);
    char *t = s + len - 1;
    while (t >= s) {
        printf("%c", *t);
        t = t - 1; //pointer arithmetic
    }
    puts("");
}

int main() {
    char *juices[] = { "dragonfruit", "waterberry", "sharonfruit", "uglifruit",
            "rumberry", "kiwifruit", "mulberry", "strawberry", "blueberry",
            "blackberry", "starfruit" };

    char *a;

    puts(juices[6]);
    print_reverse(juices[7]);
    a = juices[2];
    juices[2] = juices[8];
    juices[8] = a;
    puts(juices[8]);
    print_reverse(juices[(18 + 7) / 5]);

    puts(juices[2]);
    print_reverse(juices[9]);
    juices[1] = juices[3];
    puts(juices[10]);
    print_reverse(juices[1]);

    return 0;
}

这很奇怪,因为如果你检查下面的第三个代码,它会报错,因为指向字符串文字的指针无法更新。如果事情是这样的,那么在第二个代码中 char *juices[] 是指向字符串文字的指针数组,然后 juices[2] 是指向 “sharonfruit”,如何更新?

#include <stdio.h>

int main()
{

char *cards = "JQK";  //you should use char cards[]="JQK";
char a_card = cards[2];

cards[2] = cards[1];
cards[1] = cards[0];
cards[0] = cards[2];
cards[2] = cards[1];
cards[1] = a_card;

puts(cards);

return 0;

}

最佳答案

char tracks[][80] = { "I left my heart in Harvard Med School",
    "Newark, Newark - a wonderful town", "Dancing with a Dork",
    "From here to maternity", "The girl from Iwo Jima", };

创建 char 数组的数组,换句话说,创建 char 的二维数组,并用每个字符串初始化每个索引。字符串的数量由编译器在编译时确定,每个字符串的最大长度为 80。

char *tracks[] = { "I left my heart in Harvard Med School",
    "Newark, Newark - a wonderful town", "Dancing with a Dork",
    "From here to maternity", "The girl from Iwo Jima", };

声明一个char 指针数组。每个指针指向每个字符串文字。指针的数量由编译器在编译时确定。

字符串文字是不可变的,这意味着它无法更改。

在使用 tracks 的前两个程序中,第一个版本(char tracks[][80])和第二个版本(char* tracks[] ) 在字符串未更改且数组未分配时工作。

在使用 juices 的后两个程序中,第一个版本有效(char* juices[]),因为指针可以更改,即它们指向的位置可以更改,但第二个版本 (char juices[][80]) 不会,因为您分配 数组。请记住,数组不可分配

要解决此问题,您可以使用 string.h 库中的 strcpy 函数来交换存储在 char juices[][80] 中的字符串。请注意,如果您要尝试此操作,则需要为变量 a 分配内存。

关于c - C 中的 char *s[] 和 char s[][20] 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29447072/

相关文章:

string - 应该是VBA中的简单循环程序

用于交互式 bash 的 C 接口(interface)

algorithm - 给定一个文件,尽可能高效地找到十个最常出现的单词

java - 数组输出再重复一次就应该了

c# - 字符串长度评估不正确

python - 如何取一个数字并找到它的字母位置python

c - 如何在C中从这种类型的字符串中获取数字

C 数组指针算术

c++ - 如何检查 `strcmp`是否失败?

c - 毫不费力地想象同一种语言中的两种类型的 bool 对