C Tutorial/Language/Variable Pointer

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

Output value at the address

#include <stdio.h>
int main(void)
{
  int number = 0;                 
  int *pointer = NULL;            
  number = 10;
  pointer = &number;            
  printf("\npointer"s value: %p", pointer);  /* Output the value (an address) */
  printf("\nvalue pointed to: %d\n", *pointer);       /* Value at the address */
  return 0;
}
pointer"s value: 9a378
     value pointed to: 10

Put values in the memory locations by using pointers

#include <stdio.h>
main(){
    int a[5];
    int *b;
    int *c;
    int i;
    for(i = 0;i<5;i++){
        a[i]=i;
    }
    for(i = 0;i<5;i++)   {
        printf("value in array %d\n",a[i]);
    }
    b=a;
    b++;
    *b=4;
    b++;
    *b=6;
    b++;
    *b=8;
    b++;
    *b=10;
    b++;
    *b=12;
    printf("after\n\n\n");
    for(i = 0;i<5;i++)   {
        printf("value in array %d\n",a[i]);
    }
}
value in array 0
      value in array 1
      value in array 2
      value in array 3
      value in array 4
      after
      
      
      value in array 0
      value in array 4
      value in array 6
      value in array 8
      value in array 10