如果p
和temp
是两个指针变量,其中p
包含NULL
和temp
指向一些内存地址。两个指针变量可以指向相同的内存地址吗?
现在假设p = temp;
这意味着现在p
指向相同的地址,其中temp
指向。
这是否意味着两个指针变量p
和temp
现在指向相同的内存地址?
如果p
和temp
是两个指针变量,其中p
包含NULL
和temp
指向一些内存地址。两个指针变量可以指向相同的内存地址吗?
现在假设p = temp;
这意味着现在p
指向相同的地址,其中temp
指向。
这是否意味着两个指针变量p
和temp
现在指向相同的内存地址?
是两个指针变量可以指向相同的对象:
指针是包含一个C对象的地址,或空指针对象。
多个指针可以指向相同的对象:
char *p, *q;
p = q = "a";
指针甚至可以指向自身:
void *p;
p = &p;
这里是双重链接循环列表另一示例与单个元素:next
和prev
链接都指向相同的位置,结构本身:
struct dlist {
struct dlist *prev, *next;
int value;
} list = { &list, &list, 0 };
是的!多个指针可以指向相同的东西。
是2个指针变量可以指向相同的存储器地址。
我们知道指针是一个包含相同数据类型地址的变量。考虑下面的例子C
#include<stdio.h>
int main() {
int a,*pointer1;
a = 10;
pointer1 = &a;
int *pointer2;
printf("\n Val : %d",*pointer1); //This contains address of the variable a - so accessing it with * prints its value - value of “a"
printf("\n Val : %d", *pointer2); //This contains some junk value - so accessing it with * causes segmentation fault - since there might not be memory address with that junk value
pointer2 = pointer1; //Now pointer1 reference i.e.; address value stored in pointer1 is made a copy and now both pointer1 and pointer2 will point to the same memory location
printf("\n Val : %d", *pointer2); //Now this would print the value of “a"
return -1;
}
这同样适用于链表地址。你的变量“p”和“temp”会指向相同的内存地址!
是的,它的确也是指针用例之一。 –
这就像问“两个整数是否具有相同的值?” – harold
@harold:这是一个非2s补充体系结构的技巧问题;-) – chqrlie