Vi kan udføre aritmetiske operationer på pointerne som addition, subtraktion osv. Men da vi ved, at pointeren indeholder adressen, vil resultatet af en aritmetisk operation udført på pointeren også være en pointer, hvis den anden operand er af typen heltal. I pointer-fra-pointer subtraktion vil resultatet være en heltalsværdi. Følgende aritmetiske operationer er mulige på markøren i C-sprog:
- Forøgelse
- Nedsættelse
- Tilføjelse
- Subtraktion
- Sammenligning
Inkrementerende pointer i C
Hvis vi øger en pointer med 1, vil markøren begynde at pege på den umiddelbare næste placering. Dette er noget anderledes end den generelle aritmetik, da værdien af markøren vil blive øget med størrelsen af den datatype, som markøren peger på.
Vi kan krydse et array ved at bruge inkrementoperationen på en pointer, som vil blive ved med at pege på hvert element i arrayet, udføre en operation på det og opdatere sig selv i en løkke.
Reglen for at øge markøren er angivet nedenfor:
new_address= current_address + i * size_of(data type)
Hvor i er det tal, som markøren øges med.
32-bit
For 32-bit int-variabler vil den blive forøget med 2 bytes.
64-bit
For 64-bit int-variabler vil den blive forøget med 4 bytes.
hvordan man sletter kolonne i postgresql
Lad os se eksemplet med stigende pointervariabel på 64-bit arkitektur.
#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; }
Produktion
Address of p variable is 3214864300 After increment: Address of p variable is 3214864304
Gennemløb af et array ved hjælp af pointer
#include void main () { int arr[5] = {1, 2, 3, 4, 5}; int *p = arr; int i; printf('printing array elements... '); 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'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=&number;//stores the address of number variable printf('Address of p variable is %u ',p); p=p-1; printf('After decrement: Address of p variable is %u ',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'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=&number;//stores the address of number variable printf('Address of p variable is %u ',p); p=p+3; //adding 3 to pointer variable printf('After adding 3: Address of p variable is %u ',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'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=&number;//stores the address of number variable printf('Address of p variable is %u ',p); p=p-3; //subtracting 3 from pointer variable printf('After subtracting 3: Address of p variable is %u ',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 = &i; int *temp; temp = p; p = p + 3; printf('Pointer Subtraction: %d - %d = %d',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 & 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 = &addition; result = (*ptr)(); printf('The sum is %d',result); } int addition() { int a, b; printf('Enter two numbers?'); scanf('%d %d',&a,&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 = &arr; result1 = (**ptr)(); printf('printing the value returned by show : %d',result1); (*(*ptr+1))(result1); } int show() { int a = 65; return a++; } int showadd(int b) { printf(' Adding 90 to the value returned by show: %d',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;>
Dekrementerende pointer i C
Ligesom stigning kan vi dekrementere en pointervariabel. Hvis vi formindsker en markør, begynder den at pege på den forrige placering. Formlen for dekrementering af markøren er givet nedenfor:
new_address= current_address - i * size_of(data type)
32-bit
For 32-bit int-variabler vil den blive reduceret med 2 bytes.
64-bit
For 64-bit int-variabler vil den blive reduceret med 4 bytes.
Lad os se eksemplet med dekrementerende pointervariabel på 64-bit OS.
#include void 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 decrement: Address of p variable is %u ',p); // P will now point to the immidiate previous location. }
Produktion
Address of p variable is 3214864300 After decrement: Address of p variable is 3214864296
C Pointer tilføjelse
Vi kan tilføje en værdi til pointervariablen. Formlen for at tilføje værdi til pointeren er givet nedenfor:
new_address= current_address + (number * size_of(data type))
32-bit
For 32-bit int variabel vil den tilføje 2 * tal.
64-bit
For 64-bit int variabel vil den tilføje 4 * tal.
Lad os se eksemplet med tilføjelse af værdi til pointervariabel på 64-bit arkitektur.
#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+3; //adding 3 to pointer variable printf('After adding 3: Address of p variable is %u ',p); return 0; }
Produktion
Address of p variable is 3214864300 After adding 3: Address of p variable is 3214864312
Som du kan se, er adressen på p 3214864300. Men efter at have tilføjet 3 med p-variabelen, er den 3214864312, dvs. 4*3=12 trin. Da vi bruger 64-bit arkitektur, stiger den 12. Men hvis vi brugte 32-bit arkitektur, steg den kun til 6, dvs. 2*3=6. Som heltalsværdi optager 2-byte hukommelse i 32-bit OS.
C Pointer subtraktion
Ligesom pointeraddition kan vi trække en værdi fra pointervariablen. Hvis du trækker et hvilket som helst tal fra en pointer, får du en adresse. Formlen for at trække værdi fra pointervariablen er givet nedenfor:
new_address= current_address - (number * size_of(data type))
32-bit
For 32-bit int-variable vil den trække 2 * tal fra.
strengfunktioner i java
64-bit
For 64-bit int-variable vil den trække 4 * tal fra.
Lad os se eksemplet med at trække værdi fra pointervariablen på 64-bit arkitektur.
#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-3; //subtracting 3 from pointer variable printf('After subtracting 3: Address of p variable is %u ',p); return 0; }
Produktion
Address of p variable is 3214864300 After subtracting 3: Address of p variable is 3214864288
Du kan se efter at have trukket 3 fra pointervariablen, at den er 12 (4*3) mindre end den forrige adresseværdi.
Men i stedet for at trække et tal fra, kan vi også trække en adresse fra en anden adresse (pointer). Dette vil resultere i et tal. Det vil ikke være en simpel aritmetisk operation, men den vil følge følgende regel.
Hvis to pointere er af samme type,
Address2 - Address1 = (Subtraction of two addresses)/size of data type which pointer points
Overvej følgende eksempel for at trække en pointer fra en anden.
#include void main () { int i = 100; int *p = &i; int *temp; temp = p; p = p + 3; printf('Pointer Subtraction: %d - %d = %d',p, temp, p-temp); }
Produktion
Pointer Subtraction: 1030585080 - 1030585068 = 3
Ulovlig regnestykke med pegepinde
Der er forskellige operationer, som ikke kan udføres på pointere. Da pointer gemmer adresse, må vi derfor ignorere de operationer, der kan føre til en ulovlig adresse, for eksempel addition og multiplikation. En liste over sådanne operationer er givet nedenfor.
- Adresse + Adresse = ulovligt
- Adresse * Adresse = ulovlig
- Adresse % Adresse = ulovlig
- Adresse / Adresse = ulovlig
- Adresse & Adresse = ulovligt
- Adresse ^ Adresse = ulovlig
- Adresse | Adresse = ulovlig
- ~Adresse = ulovlig
Pointer til at fungere i C
Som vi diskuterede i det foregående kapitel, kan en pointer pege på en funktion i C. Dog skal deklarationen af pointervariablen være den samme som funktionen. Overvej følgende eksempel for at lave en markør, der peger på funktionen.
#include int addition (); int main () { int result; int (*ptr)(); ptr = &addition; result = (*ptr)(); printf('The sum is %d',result); } int addition() { int a, b; printf('Enter two numbers?'); scanf('%d %d',&a,&b); return a+b; }
Produktion
Enter two numbers?10 15 The sum is 25
Pointer til række af funktioner i C
For at forstå begrebet en række funktioner, skal vi forstå rækken af funktioner. Grundlæggende er et array af funktionen et array, der indeholder funktionernes adresser. Med andre ord er markøren til en række funktioner en markør, der peger på en matrix, som indeholder pointerne til funktionerne. Overvej følgende eksempel.
#include int show(); int showadd(int); int (*arr[3])(); int (*(*ptr)[3])(); int main () { int result1; arr[0] = show; arr[1] = showadd; ptr = &arr; result1 = (**ptr)(); printf('printing the value returned by show : %d',result1); (*(*ptr+1))(result1); } int show() { int a = 65; return a++; } int showadd(int b) { printf(' Adding 90 to the value returned by show: %d',b+90); }
Produktion
printing the value returned by show : 65 Adding 90 to the value returned by show: 155
5;>