不同创建字符串的方法在C中

huangapple go评论71阅读模式
英文:

different ways to create string in C

问题

我正在寻求理解创建字符串的每种方法的优缺点,有人可以帮助我吗?

char x[] = {'a', 'b', 'c', '
char x[] = {'a', 'b', 'c', '\0'};
'
};

或者

char x[] = "abc";

或者

char *p;
p = "abc";

我是否忘记了其他方法?谢谢。

英文:

I'm looking for understand the pros and cons of every method to create a string, can someone help me?

char x[] = {'a', 'b', 'c', '
char x[] = {'a', 'b', 'c', '\0'};
'};

or

char x[]="abc";

or

char *p;
p="abc";

Do i forgot other methods? Thank you.

答案1

得分: 2

char x[] = {'a', 'b', 'c', '\0'};
char x[] = "abc";
char *p = "abc";

英文:
char x[]={{'a'},{'b'},{'c'},{'
char x[]={{'a'},{'b'},{'c'},{'\0}};
}};

In addition to the missing apostrophe after '\0, this should normally be char x[] = {'a', 'b', 'c', '\0'};. The C grammar allows extra braces with single scalar initializers, but compilers will generally warn about this because it is indicative of an error—braces are usually used to indicate the composition of aggregate objects such as arrays or structures.

With the apostrophe fixed, this defines x to be an array of four characters, which are a, b, c, and the null character. This format is tedious, so effectively nobody defines an array this way unless there is some special circumstance, such as needing to define one of the characters using an expression or perhaps when the array is being defined for some purpose other than use as a character string and one wishes to emphasize that.

char x[]="abc";

This defines x to be the same of four characters as above. This is the usual way to define an array of characters.

There are no differences between these two initialization methods in the C semantics; both define the same array, and so the only reasons for choosing between them are how humans interact with them—which is easier to read, easier to edit without making mistakes, and so on.

char *p;
p="abc";

This is a definition and an assignment. It is better written as char *p = "abc"; unless there is reason not to do so.

This defines p to be a pointer to a static array of the same four characters as above. It is not common to use this merely to define an array because it creates a needless pointer, p. It is generally used only when one wants a pointer that initially points to the static array but that may be changed later.

答案2

得分: 0

当然,还有另一种方式。您可以使用复合文字来立即初始化或分配指针。这种初始化方式创建一个指向可修改字符串的指针。

char *p = (char[]){ "ASDF" };

int main()
{
    p[2] = 'X';
    printf("%s\n", p);
}

https://onlinegdb.com/H19BLahy8

英文:

There is another way of course as well. You can instantly initialize or assign pointers with compound literals. This kind of initialisations creates pointer to amendable string.

char *p = (char[]){"ASDF"};

int main()
{
    p[2] = 'X';
    printf("%s\n", p);
}

https://onlinegdb.com/H19BLahy8

答案3

得分: 0

>     char x[] = {'a', 'b', 'c', '
>     char x[] = {'a', 'b', 'c', '\0'};
>     char x[] = "abc";

----

using `malloc()` (and friends)

    char *p = malloc(4);
    if (!p) { /* error; */ exit(EXIT_FAILURE); }
    strcpy(p, "abc");
    free(p);

----

using [compound literal](http://port70.net/~nsz/c/c11/n1570.html#6.5.2.5) (different than a string literal)

    char *example(char *p) {
        p[0] = 'A';
        p[2] = 'C';
        return p;
    }
    example((char[4]){"abc"}); // unnamed object
'};
> char x[] = "abc"; ---- using `malloc()` (and friends) char *p = malloc(4); if (!p) { /* error; */ exit(EXIT_FAILURE); } strcpy(p, "abc"); free(p); ---- using [compound literal](http://port70.net/~nsz/c/c11/n1570.html#6.5.2.5) (different than a string literal) char *example(char *p) { p[0] = 'A'; p[2] = 'C'; return p; } example((char[4]){"abc"}); // unnamed object
英文:

> char x[] = {'a', 'b', 'c', '\0'};
> char x[] = "abc";


using malloc() (and friends)

char *p = malloc(4);
if (!p) { /* error; */ exit(EXIT_FAILURE); }
strcpy(p, "abc");
free(p);

using compound literal (different than a string literal)

char *example(char *p) {
    p[0] = 'A';
    p[2] = 'C';
    return p;
}
example((char[4]){"abc"}); // unnamed object

huangapple
  • 本文由 发表于 2020年1月3日 20:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定