指针可以指向一份普通类型的数据,也可以指向一份指针类型的数据。
一个指针指向的是另外一个指针,我们就称它为二级指针,或者指向指针的指针。
假设有一个 int 类型的变量 a,p1是指向 a 的指针变量,p2 又是指向 p1 的指针变量,它们的关系转换为C语言代码:
int a =100;
int *p1 = &a;
int **p2 = &p1;
指针变量也是一种变量,也会占用存储空间,也可以使用&获取它的地址。C语言不限制指针的级数。
代码;
#include
#include "resource.h"
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
TCHAR szClassName[] = TEXT("twoPointerDemo");
int WINAPI
WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
hInst = hThisInstance;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = MAKEINTRESOURCE (IDC_TWOPOINTERDEMO);
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
TEXT("C 二级指针Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
200,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rt;
char szBuffer[100];
int a =909;
int *p1 = &a;
int **p2 = &p1;
int ***p3 = &p2;
switch (message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_twoPointer:
hdc=GetDC(hwnd);
wsprintf(szBuffer, "%d, %d, %d, %d",a, *p1, **p2, ***p3);
TextOut(hdc,10,20,szBuffer,lstrlen(szBuffer));
wsprintf(szBuffer, "&p2 = %#X, p3 = %#X",&p2, p3);
TextOut(hdc,10,50,szBuffer,lstrlen(szBuffer));
printf("&p1 = %#X, p2 = %#X, *p3 = %#X\n", &p1, p2, *p3);
wsprintf(szBuffer, "&p1 = %#X, p2 = %#X, *p3 = %#X",&p1, p2, *p3);
TextOut(hdc,10,80,szBuffer,lstrlen(szBuffer));
wsprintf(szBuffer, " &a = %#X, p1 = %#X, *p2 = %#X, **p3 = %#X",&a, p1, *p2, **p3);
TextOut(hdc,10,110,szBuffer,lstrlen(szBuffer));
break;
case IDM_ABOUT:
MessageBox (hwnd, TEXT ("twoPointerDemo v1.0\nCopyright (C) 2020\n by bo"),
TEXT ("twoPointerDemo"), MB_OK | MB_ICONINFORMATION);
break;
case IDM_EXIT:
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
break;
case WM_CREATE:
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rt);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
运行结果;
工程;
参阅C语言中文网关于二级指针的讲解;
http://c.biancheng.net/view/2016.html
资源和头文件;
#include "resource.h"
#include
/
//
// Menu
//
IDC_TWOPOINTERDEMO MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "C 二级指针 Demo", IDM_twoPointer
MENUITEM "E&xit", IDM_EXIT
END
POPUP "&Help"
BEGIN
MENUITEM "&About ...", IDM_ABOUT
END
END
#define IDM_EXIT 10001
#define IDM_ABOUT 10002
#define IDC_TWOPOINTERDEMO 10101
#define IDD_ABOUTBOX 10102
#define IDM_twoPointer 40001