logo

Aanwijzerberekening in C

We kunnen rekenkundige bewerkingen uitvoeren op de pointers, zoals optellen, aftrekken, enz. Omdat we echter weten dat de pointer het adres bevat, zal het resultaat van een rekenkundige bewerking die op de pointer wordt uitgevoerd ook een pointer zijn als de andere operand van het type geheel getal is. Bij het aftrekken van aanwijzer-van-aanwijzer is het resultaat een geheel getal. De volgende rekenkundige bewerkingen zijn mogelijk op de aanwijzer in C-taal:

  • Verhogen
  • Verlagen
  • Toevoeging
  • Aftrekken
  • Vergelijking

Aanwijzer verhogen in C

Als we een aanwijzer met 1 verhogen, wijst de aanwijzer naar de onmiddellijk volgende locatie. Dit wijkt enigszins af van de algemene rekenkunde, aangezien de waarde van de aanwijzer toeneemt met de grootte van het gegevenstype waarnaar de aanwijzer verwijst.

jframe

We kunnen een array doorlopen door de increment-operatie op een pointer te gebruiken die naar elk element van de array blijft wijzen, daarop een bewerking uitvoert en zichzelf in een lus bijwerkt.

De regel voor het verhogen van de aanwijzer wordt hieronder gegeven:

 new_address= current_address + i * size_of(data type) 

Waar i het getal is waarmee de aanwijzer wordt verhoogd.

32-bits

Voor een 32-bits int-variabele wordt deze met 2 bytes verhoogd.

64-bits

Voor een 64-bits int-variabele wordt deze met 4 bytes verhoogd.

Laten we het voorbeeld bekijken van het verhogen van de pointervariabele op 64-bits architectuur.

 #include int main(){ int number=50; int *p;//pointer to int p=&number;//stores the address of number variable printf('Address of p variable is %u 
',p); p=p+1; printf('After increment: Address of p variable is %u 
',p); // in our case, p will get incremented by 4 bytes. return 0; } 

Uitvoer

 Address of p variable is 3214864300 After increment: Address of p variable is 3214864304 

Een array doorlopen met behulp van een pointer

 #include void main () { int arr[5] = {1, 2, 3, 4, 5}; int *p = arr; int i; printf('printing array elements...
&apos;); for(i = 0; i<5; i++) { printf('%d ',*(p+i)); } < pre> <p> <strong>Output</strong> </p> <pre> printing array elements... 1 2 3 4 5 </pre> <h2>Decrementing Pointer in C</h2> <p>Like increment, we can decrement a pointer variable. If we decrement a pointer, it will start pointing to the previous location. The formula of decrementing the pointer is given below:</p> <pre> new_address= current_address - i * size_of(data type) </pre> <h3>32-bit</h3> <p>For 32-bit int variable, it will be decremented by 2 bytes.</p> <h3>64-bit</h3> <p>For 64-bit int variable, it will be decremented by 4 bytes.</p> <p>Let&apos;s see the example of decrementing pointer variable on 64-bit OS.</p> <pre> #include void main(){ int number=50; int *p;//pointer to int p=&amp;number;//stores the address of number variable printf(&apos;Address of p variable is %u 
&apos;,p); p=p-1; printf(&apos;After decrement: Address of p variable is %u 
&apos;,p); // P will now point to the immidiate previous location. } </pre> <p> <strong>Output</strong> </p> <pre> Address of p variable is 3214864300 After decrement: Address of p variable is 3214864296 </pre> <h2>C Pointer Addition</h2> <p>We can add a value to the pointer variable. The formula of adding value to pointer is given below:</p> <pre> new_address= current_address + (number * size_of(data type)) </pre> <h3>32-bit</h3> <p>For 32-bit int variable, it will add 2 * number.</p> <h3>64-bit</h3> <p>For 64-bit int variable, it will add 4 * number.</p> <p>Let&apos;s see the example of adding value to pointer variable on 64-bit architecture.</p> <pre> #include int main(){ int number=50; int *p;//pointer to int p=&amp;number;//stores the address of number variable printf(&apos;Address of p variable is %u 
&apos;,p); p=p+3; //adding 3 to pointer variable printf(&apos;After adding 3: Address of p variable is %u 
&apos;,p); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Address of p variable is 3214864300 After adding 3: Address of p variable is 3214864312 </pre> <p>As you can see, the address of p is 3214864300. But after adding 3 with p variable, it is 3214864312, i.e., 4*3=12 increment. Since we are using 64-bit architecture, it increments 12. But if we were using 32-bit architecture, it was incrementing to 6 only, i.e., 2*3=6. As integer value occupies 2-byte memory in 32-bit OS.</p> <h2>C Pointer Subtraction</h2> <p>Like pointer addition, we can subtract a value from the pointer variable. Subtracting any number from a pointer will give an address. The formula of subtracting value from the pointer variable is given below:</p> <pre> new_address= current_address - (number * size_of(data type)) </pre> <h3>32-bit</h3> <p>For 32-bit int variable, it will subtract 2 * number.</p> <h3>64-bit</h3> <p>For 64-bit int variable, it will subtract 4 * number.</p> <p>Let&apos;s see the example of subtracting value from the pointer variable on 64-bit architecture.</p> <pre> #include int main(){ int number=50; int *p;//pointer to int p=&amp;number;//stores the address of number variable printf(&apos;Address of p variable is %u 
&apos;,p); p=p-3; //subtracting 3 from pointer variable printf(&apos;After subtracting 3: Address of p variable is %u 
&apos;,p); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Address of p variable is 3214864300 After subtracting 3: Address of p variable is 3214864288 </pre> <p>You can see after subtracting 3 from the pointer variable, it is 12 (4*3) less than the previous address value.</p> <p>However, instead of subtracting a number, we can also subtract an address from another address (pointer). This will result in a number. It will not be a simple arithmetic operation, but it will follow the following rule.</p> <p>If two pointers are of the same type,</p> <pre> Address2 - Address1 = (Subtraction of two addresses)/size of data type which pointer points </pre> <p>Consider the following example to subtract one pointer from an another.</p> <pre> #include void main () { int i = 100; int *p = &amp;i; int *temp; temp = p; p = p + 3; printf(&apos;Pointer Subtraction: %d - %d = %d&apos;,p, temp, p-temp); } </pre> <p> <strong>Output</strong> </p> <pre> Pointer Subtraction: 1030585080 - 1030585068 = 3 </pre> <h2>Illegal arithmetic with pointers</h2> <p>There are various operations which can not be performed on pointers. Since, pointer stores address hence we must ignore the operations which may lead to an illegal address, for example, addition, and multiplication. A list of such operations is given below.</p> <ul> <li>Address + Address = illegal</li> <li>Address * Address = illegal </li> <li>Address % Address = illegal</li> <li>Address / Address = illegal</li> <li>Address &amp; Address = illegal</li> <li>Address ^ Address = illegal</li> <li>Address | Address = illegal</li> <li> ~Address = illegal</li> </ul> <h2>Pointer to function in C</h2> <p>As we discussed in the previous chapter, a pointer can point to a function in C. However, the declaration of the pointer variable must be the same as the function. Consider the following example to make a pointer pointing to the function. <pre> #include int addition (); int main () { int result; int (*ptr)(); ptr = &amp;addition; result = (*ptr)(); printf(&apos;The sum is %d&apos;,result); } int addition() { int a, b; printf(&apos;Enter two numbers?&apos;); scanf(&apos;%d %d&apos;,&amp;a,&amp;b); return a+b; } </pre> </p><p> <strong>Output</strong> </p> <pre> Enter two numbers?10 15 The sum is 25 </pre> <h2>Pointer to Array of functions in C</h2> <p>To understand the concept of an array of functions, we must understand the array of function. Basically, an array of the function is an array which contains the addresses of functions. In other words, the pointer to an array of functions is a pointer pointing to an array which contains the pointers to the functions. Consider the following example.</p> <pre> #include int show(); int showadd(int); int (*arr[3])(); int (*(*ptr)[3])(); int main () { int result1; arr[0] = show; arr[1] = showadd; ptr = &amp;arr; result1 = (**ptr)(); printf(&apos;printing the value returned by show : %d&apos;,result1); (*(*ptr+1))(result1); } int show() { int a = 65; return a++; } int showadd(int b) { printf(&apos;
Adding 90 to the value returned by show: %d&apos;,b+90); } </pre> <p> <strong>Output</strong> </p> <pre> printing the value returned by show : 65 Adding 90 to the value returned by show: 155 </pre> <hr></5;>

Aanwijzer verlagen in C

Net als increment kunnen we een pointervariabele verlagen. Als we een aanwijzer verlagen, wijst deze naar de vorige locatie. De formule voor het verlagen van de wijzer wordt hieronder gegeven:

 new_address= current_address - i * size_of(data type) 

32-bits

Voor een 32-bits int-variabele wordt deze met 2 bytes verlaagd.

64-bits

Voor een 64-bits int-variabele wordt deze met 4 bytes verlaagd.

char naar int java

Laten we het voorbeeld bekijken van het verlagen van de pointervariabele op een 64-bits besturingssysteem.

 #include void main(){ int number=50; int *p;//pointer to int p=&amp;number;//stores the address of number variable printf(&apos;Address of p variable is %u 
&apos;,p); p=p-1; printf(&apos;After decrement: Address of p variable is %u 
&apos;,p); // P will now point to the immidiate previous location. } 

Uitvoer

 Address of p variable is 3214864300 After decrement: Address of p variable is 3214864296 

C Aanwijzertoevoeging

We kunnen een waarde toevoegen aan de pointervariabele. De formule voor het toevoegen van waarde aan de aanwijzer wordt hieronder gegeven:

 new_address= current_address + (number * size_of(data type)) 

32-bits

Voor een 32-bits int-variabele wordt 2 * getal toegevoegd.

64-bits

Voor een 64-bits int-variabele wordt een getal van 4 * toegevoegd.

Laten we het voorbeeld bekijken van het toevoegen van waarde aan de pointervariabele op 64-bits architectuur.

 #include int main(){ int number=50; int *p;//pointer to int p=&amp;number;//stores the address of number variable printf(&apos;Address of p variable is %u 
&apos;,p); p=p+3; //adding 3 to pointer variable printf(&apos;After adding 3: Address of p variable is %u 
&apos;,p); return 0; } 

Uitvoer

 Address of p variable is 3214864300 After adding 3: Address of p variable is 3214864312 

Zoals je kunt zien is het adres van p 3214864300. Maar na het optellen van 3 met de p-variabele is het 3214864312, d.w.z. 4*3=12 stappen. Omdat we een 64-bits architectuur gebruiken, wordt de waarde met 12 verhoogd. Maar als we een 32-bits architectuur gebruiken, wordt deze alleen verhoogd naar 6, d.w.z. 2*3=6. Omdat de gehele waarde 2 bytes geheugen in beslag neemt in een 32-bits besturingssysteem.

f-snaar python

C Aanwijzer aftrekken

Net als bij pointeroptelling kunnen we een waarde aftrekken van de pointervariabele. Als u een getal van een aanwijzer aftrekt, krijgt u een adres. De formule voor het aftrekken van de waarde van de pointervariabele wordt hieronder gegeven:

 new_address= current_address - (number * size_of(data type)) 

32-bits

Voor een 32-bits int-variabele wordt 2 * getal afgetrokken.

64-bits

Voor een 64-bits int-variabele wordt 4 * getal afgetrokken.

Laten we het voorbeeld bekijken van het aftrekken van de waarde van de pointervariabele op 64-bits architectuur.

 #include int main(){ int number=50; int *p;//pointer to int p=&amp;number;//stores the address of number variable printf(&apos;Address of p variable is %u 
&apos;,p); p=p-3; //subtracting 3 from pointer variable printf(&apos;After subtracting 3: Address of p variable is %u 
&apos;,p); return 0; } 

Uitvoer

 Address of p variable is 3214864300 After subtracting 3: Address of p variable is 3214864288 

U kunt zien dat nadat u 3 van de pointervariabele heeft afgetrokken, dit 12 (4*3) minder is dan de vorige adreswaarde.

In plaats van een getal af te trekken, kunnen we echter ook een adres van een ander adres (pointer) aftrekken. Dit resulteert in een getal. Het zal geen eenvoudige rekenkundige bewerking zijn, maar het zal de volgende regel volgen.

Als twee pointers van hetzelfde type zijn,

 Address2 - Address1 = (Subtraction of two addresses)/size of data type which pointer points 

Beschouw het volgende voorbeeld om de ene aanwijzer van de andere af te trekken.

 #include void main () { int i = 100; int *p = &amp;i; int *temp; temp = p; p = p + 3; printf(&apos;Pointer Subtraction: %d - %d = %d&apos;,p, temp, p-temp); } 

Uitvoer

 Pointer Subtraction: 1030585080 - 1030585068 = 3 

Illegale rekenkunde met wijzers

Er zijn verschillende bewerkingen die niet op pointers kunnen worden uitgevoerd. Omdat de pointer het adres opslaat, moeten we de bewerkingen negeren die tot een illegaal adres kunnen leiden, bijvoorbeeld optellen en vermenigvuldigen. Hieronder vindt u een lijst van dergelijke bewerkingen.

string methoden
  • Adres + Adres = illegaal
  • Adres * Adres = illegaal
  • Adres % Adres = illegaal
  • Adres / Adres = illegaal
  • Adres & Adres = illegaal
  • Adres ^ Adres = illegaal
  • Adres | Adres = illegaal
  • ~Adres = illegaal

Aanwijzer naar functie in C

Zoals we in het vorige hoofdstuk hebben besproken, kan een pointer naar een functie in C verwijzen. De declaratie van de pointervariabele moet echter hetzelfde zijn als de functie. Beschouw het volgende voorbeeld om een ​​pointer te maken die naar de functie verwijst.

 #include int addition (); int main () { int result; int (*ptr)(); ptr = &amp;addition; result = (*ptr)(); printf(&apos;The sum is %d&apos;,result); } int addition() { int a, b; printf(&apos;Enter two numbers?&apos;); scanf(&apos;%d %d&apos;,&amp;a,&amp;b); return a+b; } 

Uitvoer

 Enter two numbers?10 15 The sum is 25 

Verwijzing naar een reeks functies in C

Om het concept van een reeks functies te begrijpen, moeten we de reeks functies begrijpen. Kortom, een array van de functie is een array die de adressen van functies bevat. Met andere woorden, de pointer naar een array van functies is een pointer die verwijst naar een array die de pointers naar de functies bevat. Beschouw het volgende voorbeeld.

 #include int show(); int showadd(int); int (*arr[3])(); int (*(*ptr)[3])(); int main () { int result1; arr[0] = show; arr[1] = showadd; ptr = &amp;arr; result1 = (**ptr)(); printf(&apos;printing the value returned by show : %d&apos;,result1); (*(*ptr+1))(result1); } int show() { int a = 65; return a++; } int showadd(int b) { printf(&apos;
Adding 90 to the value returned by show: %d&apos;,b+90); } 

Uitvoer

 printing the value returned by show : 65 Adding 90 to the value returned by show: 155