C/Pointer/Pointer Int

Материал из C\C++ эксперт
Перейти к: навигация, поиск

A simple program using pointers

#include <stdio.h>
void main()
{
   int number = 0;      
   int *pointer = NULL; /* A pointer that can point to type int     */
   number = 10;                     
   printf("\n number"s address: %p", &number);   
   printf("\n number"s value: %d\n\n", number);  
   pointer = &number;     /* Store the address of number in pointer   */
   printf("pointer"s address: %p", &pointer);   /* Output the address */
   printf("\npointer"s size: %d bytes", sizeof(pointer));  /* Output the size   */
   printf("\npointer"s value: %p", pointer);   /* Output the value (an address) */  
   printf("\nvalue pointed to: %d\n", *pointer);        /* Value at the address */
}


Assign an int value using a pointer

  
#include <stdio.h>
int main(void)
{
  int *p, q;
  p = &q; /* get q"s address */
  *p = 199; /* assign q a value using a pointer */
  printf("q"s value is %d", q);
  return 0;
}


Increase value vs address

  
#include <stdio.h>
int main(void)
{
  int *p, q;
  p = &q;
  q = 1;
  printf("%p ", p);
  (*p)++; /* now q is incremented and p is unchanged */
  printf("%d %p", q, p);
  return 0;
}


Int data type pointer

#include <stdio.h>
main () {
    
  int i; 
  int * ia; 
  
  i = 10; 
  ia = &i; 
  
    printf ("The address of i is %8u \n", ia);
    printf ("its value is %d\n", i);
    printf ("its value is %d\n", *ia); 
    
    *ia = 50; 
    
    printf ("the value of i is %d\n", i); 
}


Int pointer

  
#include <stdio.h>
int main(void)
{
  int *p, q;
  q = 19; /* assign q 19 */
  p = &q; /* assign p the address of q */
  printf("%d", *p); /* display q"s value using pointer */
  return 0;
}


Int pointer: get the address of an int value

#include <stdio.h>
int main(void)
{
  int target, source;
  int *m;
  source = 10;
  m = &source;
  target = *m;
  printf("%d", target);
  return 0;
}


Output address other than value: ;amp

#include <stdio.h>
int main(void)
{
  int count;
  printf("this%n is a test\n", &count);
  printf("%d", count);
  return 0;
}


pointer arithmetic

  
#include <iostream>
using namespace std;
int main( )
{
 int *pi;
 float *pf;
 int an_integer;
 float a_real;
 pi = &an_integer;
 pf = &a_real;
 pi++;
 pf++;
}


Pointer based calculation

  
#include <stdio.h>
int main(void)
{
  int *p, q;
  p = &q;
  q = 1;
  printf("%p ", p);
  *p++; 
  printf("%d %p", q, p);
  return 0;
}


Pointer comptability

#include <stdio.h>
int main(void)
{
  double x = 1.1, y;
  int  *p;
  /* assign p (an integer pointer) to point to a double. */
  p = (int *) &x;
  /* The next statement does not operate as expected. */
  y = *p; /* attempt to assign y the value x through p */
  /* The following statement won"t output 1.1. */
  printf("The (incorrect) value of x is: %f", y);
  return 0;
}


Show how pointers can be used

#include <stdio.h>
int main()
{
    int   integerVariable;
    int  *integerPointer;
    integerVariable = 2;
    printf("Thing %d\n", integerVariable);
    integerPointer = &integerVariable;
    *integerPointer = 3;
    printf("Thing %d\n", integerVariable);
    printf("Thing %d\n", *integerPointer);
    return (0);
}


Swap function: Exchange the values by pointers

  
#include <stdio.h>
void swap(int *i, int *j);
int main(void)
{
  int num1, num2;
  num1 = 100;
  num2 = 800;
  printf("num1 = %d num2 = %d\n", num1, num2);
  swap(&num1, &num2);
  printf("num1 = %d num2 = %d\n", num1, num2);
  return 0;
}
/* Exchange the values by pointers. */
void swap(int *i, int *j)
{
  int temp;
  temp = *i;
  *i = *j;
  *j = temp;
}


Use printf to output variable"s address

#include <stdio.h>
int main(void)
{
  int x = 99;
  int *p1, *p2;
  p1 = &x;
  p2 = p1;
  /* print the value of x twice */
  printf("Values at p1 and p2: %d %d\n", *p1, *p2); 
  /* print the address of x twice */
  printf("Addresses pointed to by p1 and p2: %p %p", p1, p2); 
  return 0;
}


using sizeof and pointer arithmetic

  
#include <iostream>
#include <stddef.h>
using namespace std;
int main( )
{
 float fvalues[] = {15.38,12.34,91.88,11.11,22.22};
 float *pf;
 size_t fwidth;
 pf = &fvalues[0];
 fwidth = sizeof(float);
 pf = pf + fwidth;
}