我试图理解C中的指针,但我目前对以下内容感到困惑:

Char *p = "hello" 这是一个指向字符数组的char指针,从h开始。 Char p[] = "hello" 这是一个存储hello的数组。

当我把这两个变量都传递给这个函数时,有什么不同?

void printSomething(char *p)
{
    printf("p: %s",p);
}

当前回答

让我们来看看:

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

int main()
{
    char *p = "hello";
    char q[] = "hello"; // no need to count this

    printf("%zu\n", sizeof(p)); // => size of pointer to char -- 4 on x86, 8 on x86-64
    printf("%zu\n", sizeof(q)); // => size of char array in memory -- 6 on both

    // size_t strlen(const char *s) and we don't get any warnings here:
    printf("%zu\n", strlen(p)); // => 5
    printf("%zu\n", strlen(q)); // => 5

    return 0;
}

Foo *和Foo[]是不同的类型,编译器对它们的处理也不同(pointer =地址+指针类型的表示,array =指针+数组的可选长度,如果已知,例如,如果数组是静态分配的),详细信息可以在标准中找到。在运行时级别上,它们之间没有区别(在汇编程序中,好吧,几乎没有区别,请参阅下面)。

此外,在C常见问题解答中有一个相关的问题:

Q: What is the difference between these initializations? char a[] = "string literal"; char *p = "string literal"; My program crashes if I try to assign a new value to p[i]. A: A string literal (the formal term for a double-quoted string in C source) can be used in two slightly different ways: As the initializer for an array of char, as in the declaration of char a[] , it specifies the initial values of the characters in that array (and, if necessary, its size). Anywhere else, it turns into an unnamed, static array of characters, and this unnamed array may be stored in read-only memory, and which therefore cannot necessarily be modified. In an expression context, the array is converted at once to a pointer, as usual (see section 6), so the second declaration initializes p to point to the unnamed array's first element. Some compilers have a switch controlling whether string literals are writable or not (for compiling old code), and some may have options to cause string literals to be formally treated as arrays of const char (for better error catching). See also questions 1.31, 6.1, 6.2, 6.8, and 11.8b. References: K&R2 Sec. 5.5 p. 104 ISO Sec. 6.1.4, Sec. 6.5.7 Rationale Sec. 3.1.4 H&S Sec. 2.7.4 pp. 31-2

其他回答

对于这样的情况,效果是相同的:您最终传递字符串中第一个字符的地址。

声明显然是不一样的。

下面的代码为字符串和字符指针预留内存,然后将指针初始化为指向字符串中的第一个字符。

char *p = "hello";

而下面的方法只为字符串预留内存。所以它实际上可以使用更少的内存。

char p[10] = "hello";

让我们来看看:

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

int main()
{
    char *p = "hello";
    char q[] = "hello"; // no need to count this

    printf("%zu\n", sizeof(p)); // => size of pointer to char -- 4 on x86, 8 on x86-64
    printf("%zu\n", sizeof(q)); // => size of char array in memory -- 6 on both

    // size_t strlen(const char *s) and we don't get any warnings here:
    printf("%zu\n", strlen(p)); // => 5
    printf("%zu\n", strlen(q)); // => 5

    return 0;
}

Foo *和Foo[]是不同的类型,编译器对它们的处理也不同(pointer =地址+指针类型的表示,array =指针+数组的可选长度,如果已知,例如,如果数组是静态分配的),详细信息可以在标准中找到。在运行时级别上,它们之间没有区别(在汇编程序中,好吧,几乎没有区别,请参阅下面)。

此外,在C常见问题解答中有一个相关的问题:

Q: What is the difference between these initializations? char a[] = "string literal"; char *p = "string literal"; My program crashes if I try to assign a new value to p[i]. A: A string literal (the formal term for a double-quoted string in C source) can be used in two slightly different ways: As the initializer for an array of char, as in the declaration of char a[] , it specifies the initial values of the characters in that array (and, if necessary, its size). Anywhere else, it turns into an unnamed, static array of characters, and this unnamed array may be stored in read-only memory, and which therefore cannot necessarily be modified. In an expression context, the array is converted at once to a pointer, as usual (see section 6), so the second declaration initializes p to point to the unnamed array's first element. Some compilers have a switch controlling whether string literals are writable or not (for compiling old code), and some may have options to cause string literals to be formally treated as arrays of const char (for better error catching). See also questions 1.31, 6.1, 6.2, 6.8, and 11.8b. References: K&R2 Sec. 5.5 p. 104 ISO Sec. 6.1.4, Sec. 6.5.7 Rationale Sec. 3.1.4 H&S Sec. 2.7.4 pp. 31-2

在我的记忆中,数组实际上是一组指针。 例如

p[1]== *(&p+1)

是一个真实的陈述

APUE第5.14节:

char    good_template[] = "/tmp/dirXXXXXX"; /* right way */
char    *bad_template = "/tmp/dirXXXXXX";   /* wrong way*/

... 对于第一个模板,名称分配在堆栈上,因为我们使用 数组变量。然而,对于第二个名称,我们使用指针。在这种情况下,只有 指针本身的内存位于堆栈上;编译器将字符串安排为 存储在可执行文件的只读段中。当mkstemp函数尝试时 若要修改字符串,则会发生分段错误。

引用的文字与@Ciro Santilli的解释相符。

你不允许改变字符串常量的内容,也就是第一个p所指向的内容。第二个p是一个用字符串常量初始化的数组,您可以更改其内容。