小编chr*_*rry的帖子

使用指针遍历char数组

我是C语言的新手,想知道如何使用指针获取数组的每个元素。当且仅当您知道数组的大小时,这才容易。因此,让代码为:

#include <stdio.h>

int main (int argc, string argv[]) {
    char * text = "John Does Nothing";
    char text2[] = "John Does Nothing";

    int s_text = sizeof(text); // returns size of pointer. 8 in 64-bit machine
    int s_text2 = sizeof(text2); //returns 18. the seeked size.

    printf("first string: %s, size: %d\n second string: %s, size: %d\n", text, s_text, text2, s_text2);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在我要确定的大小text。为此,我发现字符串将以'\0'字符结尾。所以我写了以下函数:

int main (int argc, string argv[]) {
    char * text = "John Does …
Run Code Online (Sandbox Code Playgroud)

c arrays string pointers sizeof

6
推荐指数
1
解决办法
3万
查看次数

标签 统计

arrays ×1

c ×1

pointers ×1

sizeof ×1

string ×1