D4_pointers (pointer)

Source: Internet
Author: User

 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 Type:

#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: output address: 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;  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.