为什么这两个指针地址没有区别?

时间:2015-05-27 15:22:02

标签: c pointers

我有这个结构:

typedef struct lin_list{
    char* payload;
    struct lin_list* next;
}LinListCell, *LinList_p;

这是我的主要内容,包含向您展示我的问题所需的信息:

int main(void){
    LinList_p linlistpointer;

    char payLoad[80+1];
    strcpy(payLoad,"Test");

    printf("%X\n\n",linlistpointer);

    linlistpointer = LinListInsertFirst(linlistpointer,LinListAllocCell(payLoad));
    linlistpointer = LinListInsertFirst(linlistpointer,LinListAllocCell(payLoad));
    linlistpointer = LinListInsertFirst(linlistpointer,LinListAllocCell(payLoad));
    listLists(linlistpointer);
    linlistpointer = LinListExtractFirst(linlistpointer);
}

这些是我的主要功能。

LinList_p LinListInsertFirst(LinList_p *anchor, LinList_p newcell){
    newcell->next=anchor;
    anchor = newcell;

    printf("\nNew first Element: %X\n\n",anchor);

    printf("\nAnchor: %X\n\n",anchor);
    return anchor;  
}

LinList_p LinListAllocCell(char* payload){
    LinList_p listpointer;
    listpointer = malloc(sizeof(LinListCell));
    listpointer->payload = strdup(payload);
    listpointer->next = NULL;

    printf("New Cell:\nAdress: %X\nPayload: %s\n",listpointer, listpointer->payload);
    return listpointer;
}

void listLists(LinList_p pointer){

    int counter=1;
    LinList_p thispointer = pointer;

    printf("Listlistspointer: %X",pointer);
    printf("\n\n----------------");
    while(thispointer){
        printf("\nNow: #%d\n",counter);
        printf("Adresse: %X\n",thispointer);
        printf("Next: %X\n",thispointer->next);
        printf("Payload: %s\n",thispointer->payload);
        printf("----------------");
        thispointer=thispointer->next;
        counter++;
    }
    printf("\n\n");
}

LinList_p LinListExtractFirst(LinList_p *anchor){

    printf("%X",anchor);

    return NULL;

}

注意LinListExtractFirst到目前为止还没有完成,实际上我只想检查我的指针是否有效并且卡在那里,最后到此处。

正如您所看到的,一个是LinList_p *,另一个是LinList_p。

但是,当我尝试通过

打印出两个值时
printf("\nAdress: %X\n\n",pointer);

在这两个函数中,我在两个函数中得到完全相同的值,这与我在main函数中的地址完全相同。

任何人都可以向我解释这个吗?两者似乎都是相同的,但是我无法通过

访问结构变量
pointer->variable;

在给出LinList_p *的函数中,所以必须有区别。

我现在很困惑......

4 个答案:

答案 0 :(得分:1)

linlistpointer是一个未初始化的指针。它的值是未定义的,依赖于它的值是未定义的行为。之后的所有内容都无关紧要,因为您的程序可以执行任何操作,从编译器的角度来看,这一切都很好。

答案 1 :(得分:0)

指向结构的指针与指向其第一个成员的指针具有相同的值(ISO / IEC 9899:2011,§6.7.2.1.13)。

仅仅因为某些char*碰巧具有与struct lin_list*相同的值并不意味着您可以将其作为struct lin_list* - 指针用于所有意图和目的。

答案 2 :(得分:0)

您无法访问LinList_p*上的指针 - >变量的原因是因为编译器将LinList_p*视为指向指针的指针,无论实际上是什么地址被传入。

您似乎正在将LinList_p传递给LinListExtractFirst,而LinList_p*需要LinListExtractFirst。这不会起作用。您必须更改LinList_p的签名才能获得linlistpointer或传递linlistpointer = LinListExtractFirst(&linlistpointer);的地址:

linlistpointer

取决于你想要它做什么。

此外,var text = "<div>The Quick brown fox ran through it's forest darkly!</div>" text = text.replace(/\<(.*?)\>/g, ' <$1> '); console.log(text.match(/\w+|\S+/g)); // ## Credit to George Lee ## 永远不会在您的示例中初始化。

答案 3 :(得分:0)

您的代码不正确。因为它会给你分段错误。 你问题的语言含糊不清。但是想知道为什么这两个打印在以下功能。您正在修改后打印两个printf("\nNew first Element: %X\n\n",anchor);值。

如果我的问题正确,LinList_p LinListInsertFirst(LinList_p *anchor, LinList_p newcell){ newcell->next=anchor; anchor = newcell; printf("\nNew first Element: %X\n\n",anchor); printf("\nAnchor: %X\n\n",anchor); return anchor; } 应该在修改锚点之前出现?

function writeToFile($, methodStr, fileName, modifyFunc) {
    return function () {
        // Whoever calls this function gets its innerhtml written to whatever
        // fileName is passed to the outer function.
        var text = $(this)[methodStr]() + "\n";
        if (typeof modifyfunc === 'function') {
            text = modifyFunc(text);
        }
        fs.appendFileSync(fileName, text);
    };
}