gpt4 book ai didi

c - “*s” 和 “s” 变量有什么区别?

转载 作者:行者123 更新时间:2023-12-02 09:09:16 25 4
gpt4 key购买 nike

如果我有这个变量:

char *s = "house";

如果我在代码的其他部分将其引用为 *ss 有什么区别?

最佳答案

how do I refer to the string as all the characters, not just the first one?

在 C 语言中,代码并不直接引用字符串的所有字符。 char *s 指向单个字符。按照标准 C 库的约定,当该字符开始字符串时,则将 s 视为指向字符串,但仍然只是字符串的第一个字符。通过检查该字符和后续字符(直到并包括空字符),代码才能了解所有字符串

char x = *s;  // x assigned the value of the first character pointed to by s.

size_t len = strlen(s); // len assigned the _length_ of the string
// Length determined by walking the string until finding a null character '\0'.

// t is assigned the same pointer value as s. Data they reference is not replicated.
// t and s both point to the same string.
char *t = s;

char u[strlen(s) + 1];
strcpy(u, s); // String data pointed to by s is coped to u.

在 C 中:“字符串是由第一个空字符终止并包含第一个空字符的连续字符序列。”

关于c - “*s” 和 “s” 变量有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54496414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com