Closed Thread
Results 1 to 3 of 3

Thread: char array

  1. #1
    RobotGymnast is offline Programmer
    Join Date
    Apr 2008
    Posts
    142
    Rep Power
    14

    char array

    I'm having problem with a char array.. here's my code.. nice and simple (I took out the unnecessary stuff), but this only gives a popup saying "a".. I've tried various other things, and keys[0] never works.. what's up with that?

    Code:
    // Test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <windows.h>
    #include <math.h>
    
    class hack
    {
    	public:
    		hack(char* hotkeys)
    		{
    			strcpy_s(keys,sizeof(keys),hotkeys);
    			char* test = "a\0";
    			test[0] = keys[0];
    			MessageBox(0,test,"keys",MB_OK);
    		};
    		~hack()
    		{
    			
    		}
    
    		char  keys[6];
    };
    
    // create an array of hacks... what they affect
    hack hacks[] =
    	{
    		hack("HP"	),
    		hack("MH"	),
    		hack("MP"	),
    		hack("M"	),
    		hack("A"	),
    		hack("D"	),
    		hack("P"	),
    		hack("R"	),
    		hack("S"	),
    		hack("J"	),
    		hack("RC"	),
    		hack("YC"	),
    		hack("HR"	),
    		hack("MR"	)
    	};
    int _tmain(int argc, _TCHAR* argv[])
    {
    	return 0;
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: char array

    I wish that code were portable and not married to MS.

    You should consider string literals to be read-only.
    Code:
    			char* test = "a\0";
    			test[0] = keys[0];
    Now if it didn't crash on my system, I might be more interested in digging further.

    For my thick-headedness, could you describe the expected behavior?

  4. #3
    RobotGymnast is offline Programmer
    Join Date
    Apr 2008
    Posts
    142
    Rep Power
    14

    Re: char array

    I got it fixed.. you're right. If I change it from a pointer to a read-only string, to an array, it works

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Reading from file char by char in array
    By cypro in forum C and C++
    Replies: 7
    Last Post: 08-10-2011, 12:30 AM
  2. help with initializing a char array
    By csepraveenkumar in forum C and C++
    Replies: 7
    Last Post: 03-23-2011, 06:17 AM
  3. Conversion from char array to int array.
    By TheUmer in forum C and C++
    Replies: 6
    Last Post: 03-26-2010, 05:33 PM
  4. Struct with char* to char array?
    By manux in forum C and C++
    Replies: 1
    Last Post: 03-17-2009, 11:19 PM
  5. a little problem with char array
    By worried_student in forum C and C++
    Replies: 2
    Last Post: 11-23-2007, 08:18 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts