-
Pointer problems
I have a pointer to a struct, when I try to give a value to a member of that struct, it doesn't work!
Here is my code --
Code:
LRESULT CALLBACK GuiSwitchProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
lpSWITCHSTRUCT lpss = (lpSWITCHSTRUCT)GetWindowLong( hwnd, 0 );
switch( msg ) {
case MCC_ADDSECTION: {
lpss->iNumChildren++;
lpss->hChildren = (HWND*)realloc( lpss->hChildren, sizeof(HWND)*lpss->iNumChildren );
lpss->lCaptions = (LPSTR*)realloc( lpss->lCaptions, sizeof(LPSTR)*lpss->iNumChildren );
lpss->iStates = (int*)realloc( lpss->iStates, sizeof(int)*lpss->iNumChildren );
lpss->lCaptions[lpss->iNumChildren-1] = (LPSTR)wParam;
lpss->iStates[lpss->iNumChildren-1] = 0;
return 0;
}
case WM_CREATE: {
lpSWITCHSTRUCT lpss = (lpSWITCHSTRUCT)malloc( sizeof( lpSWITCHSTRUCT ) );
SetWindowLong( hwnd, 0, (LONG)lpss );
return TRUE;
}
case WM_LBUTTONDOWN: {
return 0;
}
case WM_PAINT: {
PAINTSTRUCT ps;
BeginPaint( hwnd, &ps );
DrawSwitchMenu( hwnd, lpss );
EndPaint( hwnd, &ps );
return 0;
}
case WM_DESTROY: {
free(lpss);
return 0;
}
}
return DefWindowProc( hwnd, msg, wParam, lParam );
}
What am I doing wrong?
-
Code:
lpSWITCHSTRUCT lpss = (lpSWITCHSTRUCT)malloc( sizeof( lpSWITCHSTRUCT ) );
Being a pointer type, sizeof(lpSWITCHSTRUCT) == 4!
Also WM_CREATE should return 0.
-
So I should just be using sizeof ( SWITCHSTRUCT ) then?
Code:
typedef struct {
...
}SWITCHSTRUCT, *lpSWITCHSTRUCT;
-
Yep, or:
Code:
lpSWITCHSTRUCT lpss = (lpSWITCHSTRUCT)malloc( sizeof( *lpss ) );
-