为什么要使用双重间接?或者为什么使用指向指针的指针?

编程入门 行业动态 更新时间:2024-10-23 03:25:36
本文介绍了为什么要使用双重间接?或者为什么使用指向指针的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

什么时候应该在 C 中使用双重间接寻址?谁能举个例子解释一下?

When should a double indirection be used in C? Can anyone explain with a example?

我所知道的是,双重间接引用是指向指针的指针.为什么我需要一个指向指针的指针?

What I know is that a double indirection is a pointer to a pointer. Why would I need a pointer to a pointer?

推荐答案

如果你想要一个字符列表(一个单词),可以使用 char *word

If you want to have a list of characters (a word), you can use char *word

如果你想要一个单词列表(一个句子),你可以使用char **sentence

If you want a list of words (a sentence), you can use char **sentence

如果你想要一个句子列表(一个独白),你可以使用char ***monologue

If you want a list of sentences (a monologue), you can use char ***monologue

如果你想要一个独白的列表(传记),你可以使用char ****biography

If you want a list of monologues (a biography), you can use char ****biography

如果你想要一个传记列表(一个bio-library),你可以使用char *****biolibrary

If you want a list of biographies (a bio-library), you can use char *****biolibrary

如果你想要一个生物库列表(a ??lol),你可以使用char ******lol

If you want a list of bio-libraries (a ??lol), you can use char ******lol

... ...

是的,我知道这些可能不是最好的数据结构

一个非常非常无聊的用法示例lol

Usage example with a very very very boring 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 ", wordsinlol(lol)); free(lol); free(biolibrary); free(biography); free(monologue); free(sentence); free(word); }

输出:

total words in my lol: 243

更多推荐

为什么要使用双重间接?或者为什么使用指向指针的指针?

本文发布于:2023-06-01 10:27:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/412971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   要使

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!