>  기사  >  백엔드 개발  >  포인터의 예를 보여주는 C 프로그램을 작성하세요.

포인터의 예를 보여주는 C 프로그램을 작성하세요.

WBOY
WBOY앞으로
2023-09-21 19:33:02979검색

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

포인터의 특징

  • 포인터는 메모리 공간을 절약해 줍니다.

  • 포인터는 메모리 위치에 직접 액세스하기 때문에 실행 시간이 더 빠릅니다.

  • 포인터의 도움으로 메모리에 효율적으로 액세스할 수 있습니다. 즉, 메모리가 동적으로 할당 및 해제됩니다.

  • 포인터는 데이터 구조와 함께 사용됩니다.

포인터 선언

int *p;

이는 "p"가 다른 정수 변수의 주소를 보유하는 포인터 변수라는 의미입니다.

포인터 초기화

주소 연산자(&)는 포인터 변수를 초기화하는 데 사용됩니다.

예:

int qty = 175;
int *p;
p= &qty;

포인터의 예를 보여주는 C 프로그램을 작성하세요.

변수를 통해 변수 포인터에 액세스

변수의 값에 액세스하려면 간접 연산자(*)를 사용합니다.

Program

라이브 시연

#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</p><p>",sizeof (int));//4//
   printf("Address of %d is %d</p><p>",a,p);//Address value//
   printf("Value of %d is %d</p><p>",a,*p);//2//
   printf("Value of next address location of %d is %d</p><p>",a,*(p+1));//Garbage value from (p+1) address//
   printf("Address of next address location of %d is %d</p><p>",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</p><p>",sizeof(char));//1//
   printf("Address of %d is %d</p><p>",a,p0);//Address Value(p)//
   printf("Value of %d is %d</p><p>",a,*p0);//First byte of value a - 2//
   printf("Value of next address location of %d is %d</p><p>",a,*(p0+1));//Second byte of value a - 0//
   printf("Address of next address location of %d is %d</p><p>",a,(p0+1));//Address value(p)+1//
}

Output

Size of the integer is 4
Address of 2 is 6422028
Value of 2 is 2
Value of next address location of 2 is 10818512
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

위 내용은 포인터의 예를 보여주는 C 프로그램을 작성하세요.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제