Converting from type char to const char*

This is a discussion on Converting from type char to const char* within the C++ Programming forums, part of the General Programming Boards category; I'm trying to work on my loading function still. I get 10 lines into an array, from a text file, ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Converting from type char to const char*

    I'm trying to work on my loading function still. I get 10 lines into an array, from a text file, and now need to insert a colon in the right spaces, and then send each to a list. I get a windows error message when I do this, and the problem is with the strcats. Here's my attempt:

    First, here is this function:
    Code:
    void SetUp()
    {
    	for(int x=0; x<=10; x++)
    	{
            times[x] = new char[50];
    	}
    	char times[11][50];
    }
    
    void DeleteVars()
    {
    	for(int x=0; x<=10; x++)
    	{
            delete[] times[x];
    	}
    }
    And heres the problem code:

    Code:
    void LoadTimes()
    {
    	ifstream load;
    	load.open("times.txt");
    	//FILE* File;
    	//File = fopen("times.txt", "rb");
    	//if(File != NULL)
    	    //MessageBox(NULL, "works", "test", MB_OK);
    
    	for(int x=0; x<=10; x++)
    	{
    		load>>times[x];
    		//fread(&times, sizeof(char*[11]), 1, File);
    	}
    
    	//fclose(File);
    	load.close();
    
    	char* string = "";
    	char tempstring[10];
    
    	for(x=0; x<=10; x++)
    	{
    		tempstring[0] = times[x][2];
    		tempstring[1] = times[x][3];
    		tempstring[2] = times[x][4];
    		tempstring[3] = times[x][5];
    
    		strcat(string, (char*)times[x][0]);
    		strcat(string, (char*)times[x][1]);
    		strcat(string, ":");
    		strcat(string, (char*)times[x][2]);
    		strcat(string, (char*)times[x][3]);
    		strcat(string, ":");
    	    strcat(string, (char*)times[x][4]);
    		strcat(string, (char*)times[x][5]);
    
    	    SendDlgItemMessage(NULL, IDC_LIST, LB_ADDSTRING, x, (LPARAM)string);
    	}
    }
    Last edited by Leeman_s; 05-21-2003 at 10:00 PM.

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    First problem:

    Code:
    void SetUp()
    {
    	for(int x=0; x<=10; x++)
    	{
            times[x] = new char[50];
    	}
    	char times[11][50];
    }
    Somewhere, as a global variable or a class member, you already have defined char * times[11], correct? The last line does nothing, it's not needed because you have the for loop. The last line essentially temporarily creates a *second* variable called times, which only exists until the SetUp() function returns (immediately after that line).

    One big problem:

    Code:
    char* string = "";
    This, again, allocates space only for the pointer, not the string itself. You're trying to make the program write (using strcat) into memory that's not allocated. You MUST allocate space to write into, either using new and delete, or an array. You want the pointer to point to space that it can write into; as it is, it points somewhere in a data segment (where it put the one-byte string ""), somewhere you probably should NOT be writing to. If you make a pointer point at a string literal, it's very bad karma to write to it. You shouldn't be writing into the regions of memory that store the literals.

    And as for your strcat, you're going to end up with absolute garbage. There are actually multiple problems:

    1) You're casting from a character to a pointer, so it will interpret the character '1', for example, as a memory location (address 0x0000 0031).

    2) Even if you didn't have the casting problems, your string segments aren't null terminated.

    3) Tempstring does nothing.


    I'd replace that entire section of code with this:

    Code:
    char string[9];
    
    for(x=0; x<=10; x++)
    {
        wsprintf(string,"%c%c:%c%c:%c%c",times[x][0],times[x][1],times[x][2],
                                        times[x][3],times[x][4],times[x][5]);
        SendDlgItemMessage(NULL, IDC_LIST, LB_ADDSTRING, x, (LPARAM)string);
    }
    Last edited by Cat; 05-21-2003 at 11:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21