简体   繁体   English

在C中引用char数组指针

[英]Referencing char array pointers in C

I ran into a pointer dereferencing problem. 我遇到了指针引用问题。

In C, &a means the address of a. 在C中,&a表示a的地址。 If a is a pointer ,then &a simply means the address of that pointer. 如果a是指针,则&a仅表示该指针的地址。

So if we have: 因此,如果我们有:

char ptr [] = "abcd"

ptr should be a pointer pointing to the first character, which is 'a'. ptr应该是指向第一个字符“ a”的指针。 therefore, 因此,

&ptr

should be the address of ptr, which is different than the address of 'a'. 应该是ptr的地址,与“ a”的地址不同。 However, when I tried the following code I got really confused: 但是,当我尝试以下代码时,我真的很困惑:

int main()
{
    char a [] = "abcd";

    printf("0x%X 0x%X", a, &a);

}

Output: 0xBF7E62AB 0xBF7E62AB

Can someone explain why a and &a have the same value? 有人可以解释为什么a和&a具有相同的值吗? Based on my understanding they should be different. 根据我的理解,它们应该有所不同。 thanks in advance 提前致谢

So if we have: char ptr [] = "abcd" , ptr should be a pointer pointing to the first character. 因此,如果我们有: char ptr [] = "abcd" ,则ptr应该是指向第一个字符的指针。

No. Not at all. 一点都不。

ptr is an array. ptr是一个数组。 And an array is not a pointer. 而且数组不是指针。

Indeed, if you declared ptr as a real pointer, then you would get the expected behavior: 确实,如果您将ptr声明为真实指针,那么您将获得预期的行为:

const char *ptr = "abcd";
printf("ptr = %p, &ptr = %p\n", (void *)ptr, (void *)&ptr);

As to why the address of the array is the same as the address of its first element: it's quite logical. 至于为什么数组的地址与它的第一个元素的地址相同:这很合逻辑。 The array represents a contiguous sequence of elements. 数组表示元素的连续序列。 The address of the array is where the array begins in memory. 数组的地址是数组在内存中开始的位置。 It begins where its first element begins. 它从第一个元素开始的地方开始。 So, the address of the first element is (rather "can be" -- the standard does not mandate this behavior) the same as the address of the array itself. 因此,第一个元素的地址与数组本身的地址相同(而不是“可以”-标准并未强制执行此行为)。

+-----------+-----------+- - - -
| element 1 | element 2 |
+-----------+-----------+- - - -
^ start of array
^ start of first element

Can someone explain why a and &a have the same value? 有人可以解释为什么a和&a具有相同的值吗? Based on my understanding they should be different. 根据我的理解,它们应该有所不同。

In the statement 在声明中

  printf("0x%X 0x%X", a, &a);  

Both a and &a are of different types. a&a属于不同类型。 a is of char * type (after decay) while &a is of char (*)[5] type. achar *类型(衰减后),而&achar (*)[5]类型。
a decays to a pointer to the first element, therefore a is the address of first element of the string. a衰减到指向第一个元素的指针,因此a是字符串的第一个元素的地址。 While &a is the address of the string "abcd" and it is equal to the address of first element. &a是字符串"abcd"的地址,它等于第一个元素的地址。

An array is not a pointer. 数组不是指针。 That's right when you said &p is the address of the pointer, if p is definied like this: 当您说&p是指针的地址时,这是正确的,如果p是这样定义的:

char *p;

An array is different and don't has exactly the same behavior. 数组是不同的,并且行为不完全相同。

With the arrays: 使用数组:

char a[] = "abc"; , &a[0] is the address of the first element in your array, which is the same as a . &a[0]是您的阵列,其是与在所述第一元件的地址a

char a[] = "abcd" does not declare a pointer a to "abcd" but an array. char a[] = "abcd"没有声明指针a"abcd" ,但阵列。 Even if an array can decay to a pointer, it is a different type for which &a and a yield the same address. 即使一个阵列可衰减到一个指针,它是一个不同类型,其&aa产率相同的地址。

Basically a yields the address to the first element of the array, so it is equivalent to &a (and to &a[0] ). 基本上, a产生指向数组第一个元素的地址,因此它等效于&a (和&a[0] )。

数组将始终引用其在内存中的存储位置,这就是为什么当您打印a时,它会为您提供它所在的地址,这等于使指针指向数组(&a)

To get the behaveour you seek change 为了表现出品格,您寻求改变

char a [] = "abcd";

to

char *a  = strdup("abcd");

or for a readonly string 或用于只读字符串

const char *a  = "abcd";

You will then get a different address for a and &a. 然后,您将为a和&a获得一个不同的地址。

When passing an array to a function, the array gets converted to a pointer. 将数组传递给函数时,该数组将转换为指针。 With your original program try 使用原始程序尝试

printf("%d %d\n",sizeof(a),sizof(&a));

The first will vary with the size of the string, the second will be based on the pointer size on your machine. 第一个将根据字符串的大小而变化,第二个将基于计算机上的指针大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM