Heim  >  Artikel  >  Backend-Entwicklung  >  C-Programm zur Darstellung der Beziehung zwischen Zeigern

C-Programm zur Darstellung der Beziehung zwischen Zeigern

PHPz
PHPznach vorne
2023-09-08 23:45:021399Durchsuche

C-Programm zur Darstellung der Beziehung zwischen Zeigern

In der Programmiersprache C ist ein Zeiger auf einen Zeiger oder Doppelzeiger eine Variable, die die Adresse eines anderen Zeigers enthält.

Deklaration

Unten ist die Deklaration eines Zeigers auf einen Zeiger angegeben –

datatype ** pointer_name;

z. B. int **p;

Hier ist p ein Zeiger auf einen Zeiger.

Initialisierung

'&' wird zur Initialisierung verwendet.

Zum Beispiel wird

int a = 10;
int *p;
int **q;
p = &a;

Zugriff

Der indirekte Operator (*) wird verwendet, um auf

Beispielprogramm

Das Folgende ist ein C-Programm für Doppelzeiger -

Live-Demonstration
#include<stdio.h>
main ( ){
   int a = 10;
   int *p;
   int **q;
   p = &a;
   q = &p;
   printf("a =%d ",a);
   printf(" a value through pointer = %d", *p);
   printf(" a value through pointer to pointer = %d", **q);
}

Ausgabe

Wenn das obige Programm ausgeführt wird, wird das folgende Ergebnis erzeugt:

a=10
a value through pointer = 10
a value through pointer to pointer = 10

Beispiel

Betrachten Sie nun ein anderes C-Programm, das eine Zeiger-zu-Zeiger-Beziehung zeigt.

Echtzeitdemonstration

#include<stdio.h>
void main(){
   //Declaring variables and pointers//
   int a=10;
   int *p;
   p=&a;
   int **q;
   q=&p;
   //Printing required O/p//
   printf("Value of a is %d</p><p>",a);//10//
   printf("Address location of a is %d</p><p>",p);//address of a//
   printf("Value of p which is address location of a is %d</p><p>",*p);//10//
   printf("Address location of p is %d</p><p>",q);//address of p//
   printf("Value at address location q(which is address location of p) is %d</p><p>",*q);//address of a//
   printf("Value at address location p(which is address location of a) is %d</p><p>",**q);//10//
}

Ausgabe

Wenn das obige Programm ausgeführt wird, werden die folgenden Ergebnisse erzeugt:

Value of a is 10
Address location of a is 6422036
Value of p which is address location of a is 10
Address location of p is 6422024
Value at address location q(which is address location of p) is 6422036
Value at address location p(which is address location of a) is 10

Das obige ist der detaillierte Inhalt vonC-Programm zur Darstellung der Beziehung zwischen Zeigern. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen