美文网首页
c经典多级指针练习,写一个简单的集合

c经典多级指针练习,写一个简单的集合

作者: youxiaochen | 来源:发表于2019-03-19 09:45 被阅读0次
    1:经典的指针习题,求打印结果
    int main() {
        char *c[] = { "GULILAZHA", "LIYUCHUN", "LIUSHISHI", "ANJILA" };
        char **cp[] = { c + 3, c + 2, c + 1, c };
        char ***cpp = cp;
        printf("%s\n", **++cpp);
        printf("%s\n", *--*++cpp + 3);
        printf("%s\n", *cpp[-2] + 3);
        printf("%s\n", cpp[-1][-1] + 1);
        return 0;
    }
    

    结果执行是

    LIUSHISHI
    ILAZHA
    ILA
    IYUCHUN
    

    首先画一下变量在内存中的位置


    内存分布

    ++cpp,后cpp三级指针指向cp二级指针数组第一个也就是c+2的位置, *++cpp相当三级指针cpp取值得到二级指针c + 2 , c+2 相当一级指针数组c指向第二个, **++cpp, 相当再取值得到一级指针,也就是c+2当前所指向的值就是LIUSHISHI的地址,打印就是LIUSHISHI

    --++cpp+3 同上道理, ++cpp三级指针再++指向二级指针数组第二个也就是c+1位置,取值得到二级指针c+1, --*++cpp后得到二级指针c, --++cpp相当再取值,得到一级指针c的首地址值也就是GULILAZHA的地址, --++cpp+3, 相当一级指针再右加三位打印就是ILAZHA

    *cpp[-2] + 3 同上,cpp已经指向了第二个, cpp[-2],相当CPP指针回退2个步长后取值(三级指针指向的位置仍然没变),也就是二级指针c+3指向c指针数组第三个位置, *cpp[-2] 相当再取值得到一级指针也就是ANJILA, *cpp[-2] + 3再加3打印就是ILA

    cpp[-1][-1] + 1 同上 cpp[-1]得到二级指针 c+2, 指向c指针数组第2个位置, cpp[-1][-1] , 相当取指向c指针数组第1个位置,得到一级指针LIYUCHUN的地址, cpp[-1][-1] + 1 , 加1打印相当就是IYUCHUN

    2:实现一个简单的集合
    typedef void Node;
    
    typedef struct MyArrayList {
        int capacity;
        int length;
        int **nodes;//item指针数组(int* nodes[])
    }MyArrayList;
    
    MyArrayList* createList(int capacity);
    
    int length(MyArrayList *list);
    
    int add(MyArrayList *list, Node *node);
    
    int addPosition(MyArrayList *list, Node *node, int position);
    
    Node* get(MyArrayList *list, int position);
    
    int remove(MyArrayList *list, int position);
    
    int destory(MyArrayList *list);
    
    MyArrayList* createList(int capacity) {
        MyArrayList *temp = (MyArrayList*) malloc(sizeof(MyArrayList));
        if (temp != NULL){
            temp->length = 0;
            temp->capacity = capacity;
            temp->nodes = (int**)malloc(sizeof(int*)* capacity);
        }   
        return temp;
    }
    
    int length(MyArrayList *list) {
        if (list != NULL) {
            return list->length;
        }
        return -1;
    }
    
    int add(MyArrayList *list, Node *node) {
        if (list == NULL || node == NULL) {
            return -2;
        }
        return addPosition(list, node, list->length);
    }
    
    int addPosition(MyArrayList *list, Node *node, int position) {
        if (list == NULL || node == NULL || position >= list->capacity || position < 0) {
            return -2;
        }
        if (list->length >= list->capacity) {
            printf("length=%d > capacity=%d", list->length, list->capacity);
            return -1;
        }
        for (int i = list->length - 1; i >= position; i--) {
            list->nodes[i + 1] = list->nodes[i];
        }
        list->length++;
        list->nodes[position] = (int *)node;
        return 1;
    }
    
    Node* get(MyArrayList *list, int position) {
        if (list == NULL) {
            return NULL;
        }
        if (position >= list->length) {
            printf("position %d outofbounds list length%d", position, list->length);
            return NULL;
        }
        return list->nodes[position];
    }
    
    int remove(MyArrayList *list, int position) {
        if (list == NULL || position < 0) {
            return -2;
        }
        if (position >= list->length) {
            printf("position %d outofbounds list length%d", position, list->length);
            return -1;
        }
        for (int i = position; i < list->length - 1; ++i) {
            list->nodes[i] = list->nodes[i + 1];
        }
        list->nodes[list->length - 1] = NULL;
        list->length--;
        return 1;
    }
    
    int destory(MyArrayList *list) {
        if (list == NULL) {
            return -2;
        }
        free(list->nodes);
        free(list);
        return 1;
    }
    
    int main() {
        MyArrayList *list = createList(10);
        Woman w1 = { 30, "安鸡拉" };
        Woman w2 = { 33, "刘湿湿" };
        Woman w3 = { 35, "滴力喇叭" };
        Woman w4 = { 19, "咕力拉扎" };
        Woman w5 = { 29, "李欲春" };
    
        add(list, &w1);
        add(list, &w2);
        add(list, &w3);
        addPosition(list, &w4, 1);
        add(list, &w5);
        
        return 0;
    }
    

    功能简单只是练习题而已,学过的东西偶尔拿出来练练,就不会忘记了

    相关文章

      网友评论

          本文标题:c经典多级指针练习,写一个简单的集合

          本文链接:https://www.haomeiwen.com/subject/bywmmqtx.html