首页 > 其他 > 详细

d4_Pointers(指针)

时间:2014-07-24 12:09:05      阅读:262      评论:0      收藏:0      [点我收藏+]

 Pointers


     * Pointers are variables

     * Pointers store only address of the memory location.
       they do not store a value.

     * Pointers are declared like :-  int *ptr;

     * Address are always integer datatype , hence all  pointers need only 2 bytes


#include<stdio.h> void main() { int a=10; //Integer variable int *ptr; // Pointer variable ptr= &a; // address of a stored in ptr printf("%d\n",a); // prints value printf("%u\n",&a); // %u used to print address printf("%u\n",&ptr); //65526 printf("%u\n",ptr); // 65524 printf("%d\n",*ptr); //10 }

float类型:

#include<stdio.h>
void main()
{

       float b=10.24;    
       
       float * ptr; // 2bytes  float means the data type of the value present at this address

       ptr = &b;

       printf("%f\n",b);  //10.24
       printf("%u\n",&b);  // 65524
       printf("%u\n",ptr); // 65524
       printf("%u\n",&ptr); // 65528
       printf("%f\n",*ptr);  // 10.24
 
}

 * int a =10;               // a Hold the value
 * int *ptr=&a            // ptr holds the address of variable
 * int **ptr=& ptr      // ptr1 holds the address of a pointer variable

 

#include<stdio.h>
void main()
{
       int a=10;    
       int * ptr;    //single pointer    
       int ** ptr1;  //double pointer

       ptr= &a;
       ptr1= &ptr;


       printf("%d\n",a);   //10 
  
       printf("%u\n",ptr);   //65524  %u: 输出地址
       printf("%u\n",&ptr);  //65526
       printf("%u\n",*ptr);  // 10
 
       printf("%u\n",ptr1);  // 65526
       printf("%u\n",&ptr1); // 65528
       printf("%u\n",*ptr1); // 65524
       printf("%u\n",**ptr1); // 10 
}

Use of pointers:

#include<stdio.h>
void swap(int *x,int *z);

  void main()
  {
           int a=10, b=20;

            swap(&a,&b); //call by reference
  
            printf(" a = %d",a);
            printf(" b = %d",b);

  }

  void swap(int *x,int *z)
  {
          int temp;
          temp=*x;
          *x=*z;
          *z=temp;
  }

d4_Pointers(指针),布布扣,bubuko.com

d4_Pointers(指针)

原文:http://www.cnblogs.com/thrive/p/3865199.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!