포인터는 다른 변수의 주소를 저장하는 변수입니다.
포인터의 기능
-
포인터는 메모리 공간을 절약합니다.
-
메모리 위치에 직접 접근하기 때문에 포인터의 실행 시간이 더 빠릅니다.
-
포인터의 도움으로 메모리에 효율적으로 액세스합니다. 즉, 메모리가 동적으로 할당 및 해제됩니다.
-
포인터는 데이터 구조와 함께 사용됩니다.
포인터 선언
int *p;
'p'는 다른 정수 변수의 주소를 담고 있는 포인터 변수라는 뜻입니다.
포인터 초기화
주소 연산자(&)는 포인터 변수를 초기화하는 데 사용됩니다.
예를 들어,
int qty = 175; int *p; p= &qty;

포인터를 통한 변수 접근
변수의 값에 접근하기 위해 간접 연산자(*)를 사용합니다.
프로그램
#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 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