と考えてください*(*display)
。整数を設定できるように、整数のアドレスを関数に渡したい場合は、次を使用します。
void setTo7 (int *x) {
*x = 7;
}
: : :
int a = 4;
setTo7 (&a);
// a is now 7.
ポインターの値を設定したいので、ポインターをそのポインターに渡す必要があることを除いて、あなたが持っているものと違いはありません。簡単ですよね?
これを試してください:
#include <stdio.h>
#include <string.h>
static void setTo7 (int *x) { *x = 7; }
void appendToStr (char **str, char *app) {
// Allocate enough space for bigger string and NUL.
char *newstr = malloc (strlen(*str) + strlen (app) + 1);
// Only copy/append if malloc worked.
if (newstr != 0) {
strcpy (newstr, *str);
strcat (newstr, app);
}
// Free old string.
free (*str);
// Set string to new string with the magic of double pointers.
*str = newstr;
}
int main (void) {
int i = 2;
char *s = malloc(6); strcpy (s, "Hello");
setTo7 (&i); appendToStr (&s, ", world");
printf ("%d [%s]\n",i,s);
return 0;
}
出力は次のとおりです。
7 [Hello, world]
これにより、ある文字列値が別の文字列値に安全に追加され、十分なスペースが割り当てられます。二重ポインターは、インテリジェントなメモリ割り当て関数でよく使用されますが、ネイティブの文字列型があるため、C++ ではそれほど使用されませんが、他のポインターには依然として役立ちます。