char array

This is a discussion on char array within the C++ Programming forums, part of the General Programming Boards category; I'm having problem with a char array.. here's my code.. nice and simple (I took out the unnecessary stuff), but ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    67

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    test does not point to writable memory, so test[0] = keys[0] is doomed to failure.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    yeah I got it.. just make test an array and it works fine

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Yes!
    And this is typically why we recommend that string literals should be const (ie assigned to a const char* pointer):
    http://cpwiki.sourceforge.net/Common...kes_and_errors

    Oh and your "keys" array should probably be private.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    I use keys in outter code (it's not shown here, but I do)

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Then I would say your class is pretty poor encapsulation (in other words, its function is pretty shallow: it does not act much like an object, like a car, so to speak).
    Either tune it a little to make it like an object or add getter/setters. Getter/setters are great since they will protect your code if your class contents change.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    I don't care all too much about encapsulation.. I've never encountered very many reasons to use it.. if I run into problems, I will

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Then you are a bad C++ programmer and setting yourself up for a load of problems.
    Just pointing out. If you continue down that path, you will get yourself burned, one day.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Elysia View Post
    Then you are a bad C++ programmer and setting yourself up for a load of problems.
    Just pointing out. If you continue down that path, you will get yourself burned, one day.
    I agree with you unless this individual is just a hobbyist. In which case *shrug* he can just do as (s)he pleases. Though, I don't see what the big problem with encapsulation seems to be. It is kind of the whole point of having private and protected members in the first place.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Quote Originally Posted by master5001 View Post
    I agree with you unless this individual is just a hobbyist. In which case *shrug* he can just do as (s)he pleases. Though, I don't see what the big problem with encapsulation seems to be. It is kind of the whole point of having private and protected members in the first place.
    Then why use classes to begin with? Encapsulation is kind of the point of the class functionality.

    edit: If I would learn to read, then I would see you just said almost the same thing.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Naturally, and this is what I am just pointing it out.
    And it is really no fun to have to fix a lot of code that breaks because you made a public variable private or made changes to how it is handled. That is why we tend to use encapsulation in the first place.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    The power of Re-usability is awe inspiring. Ok, pointless post #1 of the day.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It made me laugh And do not worry about the not reading my post in its entirety. I call it my illiterate blond manuver. It has served me well these many years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 11:36 AM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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