Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- int basicPointers()
- {
- printf("\nbasicPointers\n");
- printf("----------------\n\n");
- int a = 8;
- int *P;
- P = &a;
- printf("The address of a is %d\n", P);
- printf("The value of Pointer P is %d\n", *P);
- printf("The address of a is %d\n", &a);
- printf("The value of a is %d\n", a);
- *P = 12;
- printf("The value of a after Pointer P changed its value is %d\n", a);
- int b = 20;
- *P = b;
- printf("The value of Pointer P is %d\n", *P);
- printf("The value of a is %d\n", a);
- printf("The address of a is %d\n", P);
- printf("The Value of b is %d\n", b);
- return 0;
- }
- int arithmaticPointers(){
- int a = 10;
- int *p;
- p = &a;
- printf("\narithmaticPointers\n");
- printf("---------------------\n\n");
- printf("Address of p is %d\n",p);
- printf("value of p is %d\n",*p);
- printf("size of integer is %d bytes\n", sizeof(int));
- printf("Address p+1 is %d\n",p+1);
- printf("value at address p+1 is %d\n",*(p+1)); //Gives unwanted behavior.
- return 0;
- }
- int pointers2Pointers(){
- printf("\npointers2Pointers\n");
- printf("-----------------\n\n");
- int x = 5;
- int* p = &x;
- *p = 6;
- int** q = &p;
- int*** r = &q;
- printf("This is the value of x: %d\n",x);
- printf("This is the value of x by using pointer p to retrieve the value of:%d\n", *p);
- printf("This is the value of x, by using the reference of **q, which uses the reference\nof p first, then p uses it's reference to retrieve the value of:%d\n", **q);
- printf("This is the value of q, by using the * reference of r to retrieve it's empty value of:%d\n", *r);
- printf("This is the value of p, by using the ** reference of r to retrieve it's empty value of:%d\n", **r);
- printf("This is the the value of x, by using the *** reference of r, it references q first, then p, uses it's reference to x to retrieve the value of:%d\n", ***r);
- ***r = 10; //We changed the value of x to 10 by using ***r
- **q = *p+2; //Changing the value of x to 10 + 2 by using **q
- printf("x = %d\n", x); //Should print 12
- return 0;
- }
- void pointerfunctionIncrement(int *p){ //*p is the value of the int at address a
- *p = (*p) + 1;
- }
- int pointerIncrement(){
- printf("\npointerIncrement\n");
- printf("-----------------\n\n");
- int a;
- a = 12;
- pointerfunctionIncrement(&a); //Address of a.
- printf("a = %d\n", a);
- }
- int pointersArrays(){
- printf("\npointersArrays\n");
- printf("-----------------\n\n");
- int a[] = {2,4,6,8,10};
- int i;
- int *p = a;
- for(i=0;i<5;i++){
- printf("The value of a[i] is: %d\n", a[i]);
- printf("The address of a[i] is: %d\n", &p[i]);
- }
- return 0;
- }
- int main(){
- basicPointers();
- arithmaticPointers();
- pointers2Pointers();
- pointerIncrement();
- pointersArrays();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement