日期:2014-05-16  浏览次数:20622 次

一篇关于指针的CSDN文章居然没看懂
本帖最后由 bluelion9527888 于 2013-01-15 17:13:55 编辑
对话Linus Torvalds:大多黑客甚至连指针都未理解
http://www.csdn.net/article/2013-01-10/2813559-two-star-programming

“不懂指针”的开发者代码示例:
typedef struct node  
{  
    struct node * next;  
    ....  
} node;  
 
typedef bool (* remove_fn)(node const * v);  
 
// Remove all nodes from the supplied list for which the   
// supplied remove function returns true.  
// Returns the new head of the list.  
node * remove_if(node * head, remove_fn rm)  
{  
    for (node * prev = NULL, * curr = head; curr != NULL; )  
    {  
        node * next = curr->next;  
        if (rm(curr))  
        {  
            if (prev)  
                prev->next = curr->next;  
            else  
                head = curr->next;  
            free(curr);  
        }  
        else  
            prev = curr;  
        curr = next;  
    }  
    return head;  


Linus Torvalds提供的解决方案:
void remove_if(node ** head, remove_fn rm)  
{  
    for (node** curr = head; *curr; )  
    {  
        node * entry = *curr;  
        if (rm(entry))  
        {  
            *curr = entry->next;  
            free(entry);  
        }  
        else  
            curr = &entry->next;
    }  

高亮的两句有什么区别吗?真心的不懂指针了~
C linux C++

------解决方案--------------------
这两个只是1维与2维指针的区别。
前者传的是1维指针