Advertisement
fatman01923

C pointers lessons from mycodeschool(up to video 7 not incl)

May 18th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5.    
  6. int basicPointers()
  7. {  
  8.     printf("\nbasicPointers\n");
  9.     printf("----------------\n\n");
  10.     int a = 8;
  11.     int *P;
  12.     P = &a;
  13.     printf("The address of a is %d\n", P);
  14.     printf("The value of Pointer P is %d\n", *P);
  15.     printf("The address of a is %d\n", &a);
  16.     printf("The value of a is %d\n", a);   
  17.     *P = 12;
  18.     printf("The value of a after Pointer P changed its value is %d\n", a);
  19.     int b = 20;
  20.     *P = b;
  21.     printf("The value of Pointer P is %d\n", *P);
  22.     printf("The value of a is %d\n", a);
  23.     printf("The address of a is %d\n", P);
  24.     printf("The Value of b is %d\n", b);
  25.     return 0;
  26. }
  27.  
  28. int arithmaticPointers(){
  29.     int a = 10;
  30.     int *p;
  31.     p = &a;
  32.     printf("\narithmaticPointers\n");
  33.     printf("---------------------\n\n");
  34.     printf("Address of p is %d\n",p);
  35.     printf("value of p is %d\n",*p);
  36.     printf("size of integer is %d bytes\n", sizeof(int));
  37.     printf("Address p+1 is %d\n",p+1);
  38.     printf("value at address p+1 is %d\n",*(p+1)); //Gives unwanted behavior.
  39.    
  40.     return 0;
  41. }
  42. int pointers2Pointers(){
  43.     printf("\npointers2Pointers\n");
  44.     printf("-----------------\n\n");
  45.     int x = 5;
  46.     int* p = &x;
  47.     *p = 6;
  48.     int** q = &p;
  49.     int*** r = &q;
  50.     printf("This is the value of x: %d\n",x);
  51.     printf("This is the value of x by using pointer p to retrieve the value of:%d\n", *p);
  52.     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);
  53.     printf("This is the value of q, by using the * reference of r to retrieve it's empty value of:%d\n", *r);
  54.     printf("This is the value of p, by using the ** reference of r to retrieve it's empty value of:%d\n", **r);
  55.     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);
  56.    
  57.     ***r = 10; //We changed the value of x to 10 by using ***r
  58.     **q = *p+2; //Changing the value of x to 10 + 2 by using **q
  59.     printf("x = %d\n", x); //Should print 12
  60.     return 0;
  61. }
  62. void pointerfunctionIncrement(int *p){ //*p is the value of the int at address a
  63.     *p = (*p) + 1;
  64. }
  65.  
  66. int pointerIncrement(){
  67.     printf("\npointerIncrement\n");
  68.     printf("-----------------\n\n");
  69.     int a;
  70.     a = 12;
  71.     pointerfunctionIncrement(&a); //Address of a.
  72.     printf("a = %d\n", a);
  73. }
  74.  
  75. int pointersArrays(){
  76.     printf("\npointersArrays\n");
  77.     printf("-----------------\n\n");
  78.     int a[] = {2,4,6,8,10};
  79.     int i;
  80.     int *p = a;
  81.     for(i=0;i<5;i++){
  82.         printf("The value of a[i] is: %d\n", a[i]);
  83.         printf("The address of a[i] is: %d\n", &p[i]);
  84.     }
  85.     return 0;
  86. }
  87. int main(){
  88.     basicPointers();
  89.     arithmaticPointers();
  90.     pointers2Pointers();
  91.     pointerIncrement();
  92.     pointersArrays();
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement