Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

C 언어를 사용하여 포인터의 개념을 보여줍니다.

<시간/>

포인터는 다른 변수의 주소를 저장하는 변수입니다.

포인터의 구문은 다음과 같습니다 -

pointer = &variable;

예시

다음은 C 언어를 사용하는 포인터 개념에 대한 C 프로그램입니다 -

#include<stdio.h>
void main(){
   //Declaring variables and pointer//
   int a=2;
   int *p;
   //Declaring relation between variable and pointer//
   p=&a;
   //Printing required example statements//
   printf("Size of the integer is %d\n",sizeof (int));//4//
   printf("Address of %d is %d\n",a,p);//Address value//
   printf("Value of %d is %d\n",a,*p);//2//
   printf("Value of next address location of %d is %d\n",a,*(p+1));//Garbage value from (p+1) address//
   printf("Address of next address location of %d is %d\n",a,(p+1));//Address value +4//
   //Typecasting the pointer//
   //Initializing and declaring character data type//
   //a=2 = 00000000 00000000 00000000 00000010//
   char *p0;
   p0=(char*)p;
   //Printing required statements//
   printf("Size of the character is %d\n",sizeof(char));//1//
   printf("Address of %d is %d\n",a,p0);//Address Value(p)//
   printf("Value of %d is %d\n",a,*p0);//First byte of value a - 2//
   printf("Value of next address location of %d is %d\n",a,*(p0+1));//Second byte of value a - 0//
   printf("Address of next address location of %d is %d\n",a,(p0+1));//Address value(p)+1//
}

출력

위의 프로그램이 실행되면 다음과 같은 결과가 생성됩니다 -

Size of the integer is 4
Address of 2 is 6422028
Value of 2 is 2
Value of next address location of 2 is 463824
Address of next address location of 2 is 6422032
Size of the character is 1
Address of 2 is 6422028
Value of 2 is 2
Value of next address location of 2 is 0
Address of next address location of 2 is 6422029