第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么要用雙重間接?或者為什么使用指針指向指針?

為什么要用雙重間接?或者為什么使用指針指向指針?

C
九州編程 2019-07-05 18:33:08
為什么要用雙重間接?或者為什么使用指針指向指針?在C中什么時候應該使用雙重間接?有人能舉個例子來解釋嗎?我所知道的是,雙重間接方向是指向指針的指針。為什么我需要一個指向指針的指針?
查看完整描述

3 回答

?
catspeake

TA貢獻1111條經(jīng)驗 獲得超0個贊

如果您想要一個字符列表(一個單詞),可以使用char *word

如果你想要一個單詞列表(一個句子),你可以用char **sentence

如果您想要一個句子列表(獨白),您可以使用char ***monologue

如果您想要一個獨白列表(傳記),您可以使用char ****biography

如果您想要一個傳記列表(一個生物庫),您可以使用char *****biolibrary

如果您想要一個生物庫列表(a?lol),可以使用char ******lol

... ...

是的,我知道這可能不是最好的數(shù)據(jù)結(jié)構(gòu)


一個非常無聊的用法示例LOL

#include <stdio.h>#include <stdlib.h>#include <string.h>int wordsinsentence(char **x) {
    int w = 0;
    while (*x) {
        w += 1;
        x++;
    }
    return w;}int wordsinmono(char ***x) {
    int w = 0;
    while (*x) {
        w += wordsinsentence(*x);
        x++;
    }
    return w;}int wordsinbio(char ****x) {
    int w = 0;
    while (*x) {
        w += wordsinmono(*x);
        x++;
    }
    return w;}int wordsinlib(char *****x) {
    int w = 0;
    while (*x) {
        w += wordsinbio(*x);
        x++;
    }
    return w;}int wordsinlol(char ******x) {
    int w = 0;
    while (*x) {
        w += wordsinlib(*x);
        x++;
    }
    return w;}int main(void) {
    char *word;
    char **sentence;
    char ***monologue;
    char ****biography;
    char *****biolibrary;
    char ******lol;

    //fill data structure
    word = malloc(4 * sizeof *word); // assume it worked
    strcpy(word, "foo");

    sentence = malloc(4 * sizeof *sentence); // assume it worked
    sentence[0] = word;
    sentence[1] = word;
    sentence[2] = word;
    sentence[3] = NULL;

    monologue = malloc(4 * sizeof *monologue); // assume it worked
    monologue[0] = sentence;
    monologue[1] = sentence;
    monologue[2] = sentence;
    monologue[3] = NULL;

    biography = malloc(4 * sizeof *biography); // assume it worked
    biography[0] = monologue;
    biography[1] = monologue;
    biography[2] = monologue;
    biography[3] = NULL;

    biolibrary = malloc(4 * sizeof *biolibrary); // assume it worked
    biolibrary[0] = biography;
    biolibrary[1] = biography;
    biolibrary[2] = biography;
    biolibrary[3] = NULL;

    lol = malloc(4 * sizeof *lol); // assume it worked
    lol[0] = biolibrary;
    lol[1] = biolibrary;
    lol[2] = biolibrary;
    lol[3] = NULL;

    printf("total words in my lol: %d\n", wordsinlol(lol));

    free(lol);
    free(biolibrary);
    free(biography);
    free(monologue);
    free(sentence);
    free(word);}

產(chǎn)出:

total words in my lol: 243


查看完整回答
反對 回復 2019-07-05
?
慕沐林林

TA貢獻2016條經(jīng)驗 獲得超9個贊

原因之一是要將傳遞給函數(shù)的指針的值更改為函數(shù)參數(shù),為此需要指針指向指針。

簡單地說,使用**當您希望保留(或保留更改)內(nèi)存分配或賦值時,即使在函數(shù)調(diào)用之外也是如此。(因此,用雙指針Arg傳遞這樣的函數(shù)。)

這可能不是一個很好的例子,但將向您展示基本用途:

void allocate(int** p){
  *p = (int*)malloc(sizeof(int));}int main(){
  int* p = NULL;
  allocate(&p);
  *p = 42;
  free(p);}


查看完整回答
反對 回復 2019-07-05
?
臨摹微笑

TA貢獻1982條經(jīng)驗 獲得超2個贊

這里有一個簡單的答案!

  • 假設(shè)您有一個指針,它的值是一個地址。
  • 但現(xiàn)在你想改變那個地址。
  • 您可以執(zhí)行pointer1=pointer2,pointer1現(xiàn)在有pointer2的地址。
  • 但!如果您想要一個函數(shù)為您做這件事,并且您希望在函數(shù)完成后結(jié)果保持不變,那么您需要做一些額外的工作,您需要一個新的pointer3來指向pointer1,并將pointer3傳遞給該函數(shù)。

  • 下面是一個有趣的示例(先看看輸出下面的內(nèi)容,了解一下!):

#include <stdio.h>int main(){

    int c = 1;
    int d = 2;
    int e = 3;
    int * a = &c;
    int * b = &d;
    int * f = &e;
    int ** pp = &a;  // pointer to pointer 'a'

    printf("\n a's value: %x \n", a);
    printf("\n b's value: %x \n", b);
    printf("\n f's value: %x \n", f);
    printf("\n can we change a?, lets see \n");
    printf("\n a = b \n");
    a = b;
    printf("\n a's value is now: %x, same as 'b'... it seems we can, but can we do it in a function? lets see... \n", a);
    printf("\n cant_change(a, f); \n");
    cant_change(a, f);
    printf("\n a's value is now: %x, Doh! same as 'b'...  that function tricked us. \n", a);

    printf("\n NOW! lets see if a pointer to a pointer solution can help us... remember that 'pp' point to 'a' \n");
     printf("\n change(pp, f); \n");
    change(pp, f);
    printf("\n a's value is now: %x, YEAH! same as 'f'...  that function ROCKS!!!. \n", a);
    return 0;}void cant_change(int * x, int * z){
    x = z;
    printf("\n ----> value of 'a' is: %x inside function, same as 'f', BUT will it be the same outside of this function? lets see\n", x);}
    void change(int ** x, int * z){
    *x = z;
    printf("\n ----> value of 'a' is: %x inside function, same as 'f', BUT will it be the same outside of this function? lets see\n", *x);}
  • 這是輸出:
 a's value: bf94c204

 b's value: bf94c208 

 f's value: bf94c20c 

 can we change a?, lets see 

 a = b 

 a's value is now: bf94c208, same as 'b'... it seems we can, but can we do it in a function? lets see... 

 cant_change(a, f); 

 ----> value of 'a' is: bf94c20c inside function, same as 'f', BUT will it be the same outside of this function? lets see

 a's value is now: bf94c208, Doh! same as 'b'...  that function tricked us. 

 NOW! lets see if a pointer to a pointer solution can help us... remember that 'pp' point to 'a' 

 change(pp, f); 

 ----> value of 'a' is: bf94c20c inside function, same as 'f', BUT will it be the same outside of this function? lets see

 a's value is now: bf94c20c, YEAH! same as 'f'...  that function ROCKS!!!.


查看完整回答
反對 回復 2019-07-05
  • 3 回答
  • 0 關(guān)注
  • 995 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號