Thread: My struct is global, isn't?

  1. #1
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310

    My struct is global, isn't?

    Code:
    typedef struct {
    	HINSTANCE hInstance;
    	HWND hListView;
    	char* szNombre;
    	char* szPC;
    } CLIENTESINFO, *LPCLIENTESINFO;
    As global:
    Code:
    CLIENTESINFO ci;
    First calling is from a child dialog proc.
    Code:
    char nom[128], pc[12];
    GetDlgItemText(hDlg, IDC_EDIT1, nom, sizeof(nom)+1);
    ci.szNombre = nom;
    GetDlgItemText(hDlg, IDC_EDIT2, pc, sizeof(pc)+1);
    ci.szPC =  pc;
    Second call is from the main WinProc:
    Code:
    char t[12];
    int total = ListView_GetItemCount(ci.hListView);
    wsprintf(t, "%i", total+1);
    InsertItem(ci.hListView, total, 0, t); // t is current position and it shows correctly
    InsertItem(ci.hListView, total, 1, ci.szNombre); // empty string
    InsertItem prototype:
    Code:
    void InsertItem(HWND hList, int iItemNo, int iSubNo, char *lpszText) { 
    	LVITEM item = {0}; 
    	item.mask = LVIF_TEXT; 
    	item.pszText = lpszText; 
    	item.iItem = iItemNo; 
    	item.iSubItem = iSubNo; 
    	if (iSubNo == 0) 
    		ListView_InsertItem(hList, &item); 
    	else 
    		ListView_SetItem(hList, &item);
    	return; 
    }
    Now my question is, why the listview always shows a empty string? Isn't the struct global? Thanks
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Your struct is global, but nom[] and pc[] are locals, and szNombre and szPC point to those locals. Your should allocate memory for szNombre and szPC using winapi/new/malloc and copy those strings to that memory.

  3. #3
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Yeah!

    I see your point, that's why I change my struct to class
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is no significant difference between struct and class in C++, so you would have the same issue with each. Also, you don't need the typedef stuff for declaring structs in C++, use the same syntax as you do for classes (unless you share your struct with C code).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM