강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

호두님의 프로필 이미지
호두

작성한 질문수

홍정모의 따라하며 배우는 C++

6.9 포인터 연산과 배열 인덱싱

[연습문제 풀이]

작성

·

185

1

<code>

#include <iostream>

using namespace std;

int main()
{
    char name[] = "Jack jack";

    const int n_name = sizeof(name) / sizeof(name[0]);

    char *ptr = name;

    // 첫 번째 시도
    int i = 0;
    while (i < n_name)
    {
        if (*(ptr + i) == '\0')
            break;
        cout << *(ptr + i);
        ++i;
    }
    cout << "##" << endl;   //널 캐릭터 확인용

    // cout << *(ptr++) << endl;
    // cout << *(ptr++) << endl;
    // cout << *(ptr++) << endl;
    // cout << *(ptr++) << endl;

    // 두 번째 시도, 굳이 조건이 n_name이 아니어도 됐을 텐데...
    while (n_name)
    {
        if (*ptr == '\0')
            break;
        cout << *(ptr++);
    }
    cout << "##" << endl;   //널 캐릭터 확인용



    // for (int i = 0; i < n_name; ++i)
    // {
    //     // cout << char(toupper(name[i])) << endl;
    //     cout << *(ptr + i);
    // }
    // cout << "##" << endl;


    // cout << endl;
    // long long array[] = {9, 7, 5, 3, 1};

    // long long *ptr = array;

    // for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)
    // {
    //     cout << *(ptr + i) << " " << (uintptr_t)(ptr + i) << endl;

    // }

    // short value = 7;
    // short *ptr = &value;

    // cout << uintptr_t(ptr - 1) << endl;
    // cout << uintptr_t(ptr) << endl;
    // cout << uintptr_t(ptr + 1) << endl;
    return 0;
}

<output>

PS C:\coding\tbc_review\TBCPP\Chapter6> g++ .\6_9.cpp

PS C:\coding\tbc_review\TBCPP\Chapter6> .\a.exe      

Jack jack##

Jack jack##

답변

답변을 기다리고 있는 질문이에요
첫번째 답변을 남겨보세요!
호두님의 프로필 이미지
호두

작성한 질문수

질문하기