10. Basic of pointers

Source code

変数のアドレスの表示

#include <stdio.h>
int main(void)
{
    int mydt = 1234;
    printf("The value of mydt is %d\n", mydt);
    printf("The address of mydt is %p\n", &mydt);
    return 0;
}

ポインタの宣言と初期化

#include <stdio.h>
int main(void)
{
    int *p;
    int mydt = 1234;
    p = &mydt;
    printf("The value of mydt is %d\n", mydt);
    printf("The address of mydt is %p\n", &mydt);
    printf("The value of p is %p\n", p);
    printf("The value of *p is %d\n", *p);
    return 0;
}

ポインタと配列

#include <stdio.h>
int main(void)
{
    int ary[5] = {100, 200, 300, 400, 500};
    int *p;

    p = &ary[0];
    printf("pt=%p\n", p);
    p = ary;
    printf("pt=%p\n", p);
    return 0;
}

ポインタ演算

#include <stdio.h>
int main(void)
{
    int n, ary[3] = {100, 110, 120};
    int *pt = ary;
    n = *pt; // nは100になる
    printf("%d\n", n);
    n = *(pt + 1); // nは110になる
    printf("%d\n", n);
    n = *(pt + 2); // nは120になる
    printf("%d\n", n);
    return 0;
}

Problem

Problem 1. A~Cを適切に埋め,プログラムを実行せよ.

#include <stdio.h>
int main(void)
{
    int aa, bb;
    int *pt;
    aa = 123;
    pt = /*A 変数aaのアドレスを設定*/
    bb = /*B ポインタptの指すアドレスにある値を設定*/
    printf("aa=%d *pt=%d bb=%d\n", aa, *pt, bb);
    pt = /*C 変数bbのアドレスを設定*/
    *pt = 999;
    printf("aa=%d *pt=%d bb=%d\n", aa, *pt, bb);
    return 0;
}

Problem 2. 下の?に適当なプログラムを書いて,キーボードから入力されたxの値をyにもコピーせよ.ただし,y=x; を書いてはいけない.

#include <stdio.h>
int main(void)
{
    int x, y, *p;
    printf("x=");
    // ??
    // ??
    // ??
    printf("x=%d y=%d\n", x, y); // 同じ値の結果を出す
    return 0;
}

Problem 3. 下線部を埋めて,入力した5文字が逆順で表示されるようにせよ.ただし,sを使ってはいけない.pとiは使ってよい.

#include <stdio.h>
int main(void)
{
    int i;
    char s[6], *p;
    printf("Input 5 characters: ");
    fgets(s, 6, stdin);
    p = s;
    for (i = 0; i < 5; i++)
    {
        printf("%c", ________); /* p[4-i]でも良い */
    }
    printf("\n");
    return 0;
}

Problem 4. 入力した文字列を一文字ずつ表示させるプログラムを作れ.ただし,ポインタを使うこと.

#include <stdio.h>
int main(void)
{
    char s[20], *p;
    printf("Input a string(max.19): ");
    fgets(s, 20, stdin);
    // ???
    // ???
	// ???
    // ???
	// ???
    // ???
    return 0;
}

Solutions

#include <stdio.h>
int main(void)
{
    int aa, bb;
    int *pt;
    aa = 123;
    pt = &aa; /*変数aaのアドレスを設定*/
    bb = *pt; /*ポインタptの指すアドレスにある値を設定*/
    printf("aa=%d *pt=%d bb=%d\n", aa, *pt, bb);
    pt = &bb; /*変数bbのアドレスを設定*/
    *pt = 999;
    printf("aa=%d *pt=%d bb=%d\n", aa, *pt, bb);
    return 0;
}
#include <stdio.h>
int main(void)
{
    int x, y, *p;
    printf("x=");
    scanf("%d", &x);
    p = &x;
    y = *p;
    printf("x=%d y=%d\n", x, y); // 同じ値の結果を出す
    return 0;
}
#include <stdio.h>
int main(void)
{
    int i;
    char s[6], *p;
    printf("Input 5 characters: ");
    fgets(s, 6, stdin);
    p = s;
    for (i = 0; i < 5; i++)
    {
        printf("%c", *(p + 4 - i)); /* p[4-i]でも良い */
    }
    printf("\n");
    return 0;
}
#include <stdio.h>
int main(void)
{
    char s[20], *p;
    printf("Input a string(max.19): ");
    fgets(s, 20, stdin);
    p = s; /* p = &s[0]と同じ */
    while (*p != '\0')
    {
        printf("%c ", *p);
        p++;
    }
    return 0;
}

Last updated