Thread: Pointer problems

  1. #1

    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?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    lpSWITCHSTRUCT lpss = (lpSWITCHSTRUCT)malloc( sizeof( lpSWITCHSTRUCT ) );
    Being a pointer type, sizeof(lpSWITCHSTRUCT) == 4!
    Also WM_CREATE should return 0.

  3. #3
    So I should just be using sizeof ( SWITCHSTRUCT ) then?

    Code:
    typedef struct {
      ...
    }SWITCHSTRUCT, *lpSWITCHSTRUCT;

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Yep, or:
    Code:
    lpSWITCHSTRUCT lpss = (lpSWITCHSTRUCT)malloc( sizeof( *lpss ) );

  5. #5
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM