[英]Can two pointer variables point to the same memory Address?
如果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 };
是的,它確實! 多個指針可以指向同一個東西。
是的兩個指針變量可以指向相同的內存地址。
我們知道指針是一個包含相同數據類型地址的變量。 請考慮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”現在指向相同的內存地址!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.