指向指针数组的指针

指向指针数组的指针

#define SIZE 1024
// 先声明一个指向指针的指针
buffdesc_t * * point_array;
// 声明一个指针数组,并且返回其指针,因此指针指向了指针数组
// 即 指向指针数组的指针
point_array = malloc(SIZE * sizeof(struct buffdesc *));

具体探索小实例

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

#define likely(x)       __builtin_expect(!!(x), 1)
#define unlikely(x)     __builtin_expect(!!(x), 0)

struct buffdesc {
	void *addr;		/*< First octet/byte of the buffer */
	size_t len;		/*< Length of the buffer */
};

typedef struct buffdesc buffdesc_t;


#define SIZE 2000

// buffdesc_t * point_array[SIZE];
// 最好不要放在bss段,而是放在堆中比较好,所以等会再malloc
buffdesc_t * * point_array;
buffdesc_t * * array_cur_pos;
// 自己提前加括号就不对了,这是因为加了括号之后前面的东西变成了强制类型转换
// 比如下面做的更小的test
// (char *) p; //error: 应输入标识符
// char * p; // yes
// (buffdesc_t *) * point_array[SIZE];
// (buffdesc_t *) * array_cur_pos;
size_t ip_array_max_len = SIZE;
size_t ip_array_len = 0;

static inline int ip_array_add(buffdesc_t *new_key) {
    if (unlikely(++ip_array_len >= ip_array_max_len)) {
        struct buffdesc * * newBuffer = realloc(point_array,
            ip_array_max_len += SIZE);

        if (unlikely(newBuffer == NULL)) {
            puts("nfs-statistics realloc error");
            // free_buff_and_hashtable();
            return -1;
        }
        
        array_cur_pos = newBuffer + 
            (array_cur_pos - point_array);
    }
    *array_cur_pos++ = new_key;

    return 0;
}

void for_each_ip() {
    struct buffdesc ** begin = point_array;
    int i = 1;
    for (; begin < array_cur_pos; begin++,i++) {
        printf("第 %d 个ip, ip地址是 %s, 它的长度是 %d. \n", i, (*begin)->addr, (*begin)->len);
    }
}


int main() {
    // point_array = malloc(SIZE * 
        // sizeof(struct buffdesc *));
    // array_cur_pos = point_array;
    point_array = malloc(SIZE * 
        sizeof(struct buffdesc *));
    array_cur_pos = point_array;
    if (unlikely(array_cur_pos == NULL)) {
        puts("error:  fail to alloc nfs-statistics ip array");
        return -1;
    }

    struct buffdesc buffkey;
    char* pipaddr = (char*)malloc(strlen("127.0.0.1")+1);
    strcpy(pipaddr,"127.0.0.1");

    buffkey.addr = (char *) pipaddr;
    buffkey.len = strlen(pipaddr);
    printf("buffkey.addr is %s, and buffkey.len is %d. \n", buffkey.addr, buffkey.len);

    ip_array_add(&buffkey);

    struct buffdesc buffkey2;
    pipaddr = (char*)malloc(strlen("121.131.197.97")+1);
    strcpy(pipaddr,"121.131.197.97");

    buffkey2.addr = (char *) pipaddr;
    buffkey2.len = strlen(pipaddr);
    printf("buffkey.addr is %s, and buffkey.len is %d. \n", buffkey2.addr, buffkey2.len);

    ip_array_add(&buffkey2);

    for_each_ip();

    return 0;
}
输出

buffkey.addr is 127.0.0.1, and buffkey.len is 9.
buffkey.addr is 121.131.197.97, and buffkey.len is 14.
第 1 个ip, ip地址是 127.0.0.1, 它的长度是 9.
第 2 个ip, ip地址是 121.131.197.97, 它的长度是 14.

欢迎访问个人站

www.wolfdan.cn

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值