《征服C指针》源代码(第二章)

代码2-1

#include <stdio.h>

int hoge;

int main(void)
{
    char        buf[256];

    printf("&hoge...%p\n", (void*)&hoge);

    printf("Input initial value.\n");
    fgets(buf, sizeof(buf), stdin);
    sscanf(buf, "%d", &hoge);

    for (;;) {
        printf("hoge..%d\n", hoge);
        /*
         * getchar()で入力待ちの状態にする.
         * リターンキーを叩くごとに,hogeの値が増加する.
         */
        getchar();
        hoge++;
    }

    return 0;
}

代码2-2

#include <stdio.h>
#include <stdlib.h>

int             global_variable;
static int      file_static_variable;

void func1(void)
{
    int func1_variable;
    static int local_static_variable;

    printf("&func1_variable..%p\n", (void*)&func1_variable);
    printf("&local_static_variable..%p\n", (void*)&local_static_variable);
}

void func2(void)
{
    int func2_variable;

    printf("&func2_variable..%p\n", (void*)&func2_variable);
}

int main(void)
{
    int *p;

    /* 関数へのポインタの表示 */
    printf("func1..%p\n", (void*)func1);
    printf("func2..%p\n", (void*)func2);
    
    /* 文字列リテラルのアドレスの表示 */
    printf("string literal..%p\n", (void*)"abc");

    /* グローバル変数のアドレスの表示 */
    printf("&global_variable..%p\n", (void*)&global_variable);

    /* ファイル内static変数のアドレスの表示 */
    printf("&file_static_variable..%p\n", (void*)&file_static_variable);

    /* ローカル変数の表示 */
    func1();
    func2();

    /* mallocにより確保した領域のアドレス */
    p = malloc(sizeof(int));
    printf("malloc address..%p\n", (void*)p);

    return 0;
}
代码2-3
#include <stdio.h>

void func(int a, int b)
{
    int c, d;

    printf("func:&a..%p &b..%p\n", (void*)&a, (void*)&b);
    printf("func:&c..%p &d..%p\n", (void*)&c, (void*)&d);
}

int main(void)
{
    int a, b;

    printf("main:&a..%p &b..%p\n", (void*)&a, (void*)&b);
    func(1, 2);
    
    return 0;
}

代码2-4

#include <stdio.h>

void hello(void)
{
    fprintf(stderr, "hello!\n");
}

void func(void)
{
    void        *buf[10];
    static int  i;

    for (i = 0; i < 100; i++) {
        buf[i] = hello;
    }
}

int main(void)
{
    int buf[1000];

    func();

    return 0;
}

代码2-5

#include <stdio.h>
#include <stdarg.h>
#include <assert.h>

void tiny_printf(char *format, ...)
{
    int i;
    va_list     ap;

    va_start(ap, format);
    for (i = 0; format[i] != '\0'; i++) {
        switch (format[i]) {
          case 's':
            printf("%s ", va_arg(ap, char*));
            break;
          case 'd':
            printf("%d ", va_arg(ap, int));
            break;
          default:
            assert(0);
        }
    }
    va_end(ap);
    putchar('\n');
}

int main(void)
{
    tiny_printf("sdd", "result..", 3, 5);

    return 0;
}

代码2-6

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#define DATA_SIZE       (50000)

#define SWAP(a, b) {int temp; temp = a; a = b; b = temp;}

void quick_sort_sub(int *data, int left, int right)
{
    int left_index = left;
    int right_index = right;
    int pivot = data[(left + right) / 2];

    while (left_index <= right_index) {
        for ( ; data[left_index] < pivot; left_index++)
            ;
        for ( ; data[right_index] > pivot; right_index--)
            ;

        if (left_index <= right_index) {
            SWAP(data[left_index], data[right_index]);
            left_index++;
            right_index--;
        }
    }

    if (right_index > left) {
        quick_sort_sub(data, left, right_index);
    }
    if (left_index < right) {
        quick_sort_sub(data, left_index, right);
    }
}

void quick_sort(int *data, int data_size)
{
    quick_sort_sub(data, 0, data_size - 1);
}

void check(int *data, int size)
{
    int i;

    for (i = 0; i < size - 1; i++) {
        if (data[i] > data[i+1])
            fprintf(stderr, "error!\n");
    }
    fprintf(stderr, "ok!\n");
}

int main(void)
{
    int i;
    int data[DATA_SIZE];

    for (i = 0; i < DATA_SIZE; i++) {
        data[i] = rand();
    }

    quick_sort(data, DATA_SIZE);

    check(data, DATA_SIZE);

    return 0;
}

代码2-7

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int *int_p;

    int_p = malloc(sizeof(int));

    *int_p = 12345;

    free(int_p);

    printf("*int_p..%d\n", *int_p);

    return 0;
}

代码2-8

#include <stdio.h>

typedef struct {
    int     int1;
    double  double1;
    char    char1;
    double  double2;
} Hoge;

int main(void)
{
    Hoge        hoge;

    printf("hoge size..%d\n", sizeof(Hoge));

    printf("hoge   ..%p\n", &hoge);
    printf("int1   ..%p\n", &hoge.int1);
    printf("double1..%p\n", &hoge.double1);
    printf("char1  ..%p\n", &hoge.char1);
    printf("double2..%p\n", &hoge.double2);

    return 0;
}

代码2-9

#include <stdio.h>

int main(void)
{
    int         hoge = 0x12345678;
    unsigned char       *hoge_p = (unsigned char*)&hoge;

    printf("%x\n", hoge_p[0]);
    printf("%x\n", hoge_p[1]);
    printf("%x\n", hoge_p[2]);
    printf("%x\n", hoge_p[3]);

    return 0;
}


阅读更多
换一批

没有更多推荐了,返回首页