C 언어 함수의 활용과 상수 포인터

print_array_reverse는 double형 포인터 변수를 받아 배열의 끝에서 부터 접근해서
출력해주는 함수이다.

여기서 중요한 개념이 있는데,
상수 포인터이다.

  • const double* pArray는 포인터 변수가 가리키는 변수를 상수화 한다는 의미로

포인터 변수는 다른 주소를 가리킬수 있지만, 가리키고 있는 값을 변경할 수 없게된다.
ex) const int* pint_value = &int_value;

pint_value = &int_value2;  //가능함!!
*pint_value = 10;

  • double* const pArray처럼 const위치가 바뀐다면 포인터 변수의 상수화로

pArray는 다른 주소를 가리킬수 없게된다.
ex)int* const pint_value = &int_value;

pint_value = &int_value2; // 오류 발생!!
*pint_value = 10;            // 가능함!!


#include <stdio.h>

void print_array_reverse(const double* pArray, int size);

int main()
{
        double double_array[] = {10.0 , 20.1, 30.2, 40.3};
        int size = sizeof(double_array) / sizeof(double);

        print_array_reverse(double_array, size);

        return 0;
}


void print_array_reverse(const double* pArray, int size)
{
        int i =0;
        const double* pdouble = NULL;

        if(NULL != pArray && 0 < size)
        {
                pdouble = pArray + size -1;
                for( i = 0; i< size; i++)
                {
                        printf("[%d] %5.2f \n", i, *pdouble);
                        pdouble--;
                }
        }
        else
        {
                printf("매개변수로 NULL 혹은 원소의 개수가 0개 입니다\n");
        }
}




실행시 :
ndgndg91@LAPTOP-CCFK7MKV:~$ ./practice3
[0] 40.30
[1] 30.20
[2] 20.10
[3] 10.00

댓글

이 블로그의 인기 게시물

About JVM Warm up

About idempotent

About Kafka Basic

About ZGC

sneak peek jitpack

Spring Boot Actuator readiness, liveness probes on k8s

About Websocket minimize data size and data transfer cost on cloud

About G1 GC

대학생 코딩 과제 대행 java, python, oracle 네 번째