char copy

This is a discussion on char copy within the C Programming forums, part of the General Programming Boards category; how can i copy a single character ie. "a" to a char array. say it is char[256] i want to ...

  1. #1
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49

    char copy

    how can i copy a single character ie. "a" to a char array.

    say it is

    char[256]

    i want to copy each character to the string one piece at a time.

    thanks.
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'm not exactly clear what you mean, but here's some ideas.
    One way:
    Code:
    char name[256];
    strcpy(name,"a");
    Or another way:
    Code:
    char name[256];
    name[0] = 'a';
    name[1] = '\0';

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>how can i copy a single character ie. "a" to a char array.
    >>i want to copy each character to the string one piece at a time.
    What do you mean, "one piece at a time", you only specified one character ("a")?!
    Think about what you want to do, write down the logic on paper, then try to write the code. Paste it here when you have trouble with it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    If its an entire string why not just use strcpy? It basically copies each character one at a time than adds the null character on the end.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    iam not sure about what u mean. i hope this could help you

    Code:
    for(i=0;i<256;i++)
    name[i]='a';
    hope this helps

  6. #6
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    well i am trying to write a chatting prgm for some experience. and i needed a non-blocking keyboard system. so i used allegro for that. now i want as the user enters keys for them to be entered into the character array.

    ie. user enters "a" "g" and "f"
    i want the prgm to store "a" then "g" then "f".

    heres the code:
    Code:
    #include <allegro.h>
    #include <winalleg.h>
    
    int getkeypress( void );
    
    int getkeypress()
    {
         int tempchar;
         switch(readkey() >> 8)
         {
                          case KEY_A:   return 1;   break;
                          case KEY_B:   return 2;   break;
                          case KEY_C:   return 3;   break;
                          case KEY_D:   return 4;   break;
                          case KEY_E:   return 5;   break;
                          case KEY_F:   return 6;   break;
                          case KEY_G:   return 7;   break;
                          case KEY_H:   return 8;   break;
                          case KEY_I:   return 9;   break;
                          case KEY_J:   return 10;  break;
                          case KEY_K:   return 11;  break;
                          case KEY_L:   return 12;  break;
                          case KEY_M:   return 13;  break;
                          case KEY_N:   return 14;  break;
                          case KEY_O:   return 15;  break;
                          case KEY_P:   return 16;  break;
                          case KEY_Q:   return 17;  break;
                          case KEY_R:   return 18;  break;
                          case KEY_S:   return 19;  break;
                          case KEY_T:   return 20;  break;
                          case KEY_U:   return 21;  break;
                          case KEY_V:   return 22;  break;
                          case KEY_W:   return 23;  break;
                          case KEY_X:   return 24;  break;
                          case KEY_Y:   return 25;  break;
                          case KEY_Z:   return 26;  break;
                          
                          case KEY_SPACE:       return 98;  break;
                          case KEY_BACKSPACE:   return 99;  break;
                          case KEY_ENTER:       return 100; break;
                          }
         if ((key[KEY_LSHIFT] && key[KEY_1]) || (key[KEY_RSHIFT] && key[KEY_1]))    return 97;
         
         return 0;
    }
    
    int main (void) 
    {
        WORD sockVersion;
    	WSADATA wsaData;
    	int nret;
    	char buffer[256];
    	int tempstore;
    	int tx = 5;
    	int ty = 5;
    	int tspacing = 7;
    	int bufnum = 1;
    	
         allegro_init();
         set_color_depth(16);
         set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
         install_keyboard();
    	
    	sockVersion = MAKEWORD(1, 1);
    	
    	WSAStartup(sockVersion, &wsaData);
    	
    	SOCKET mysocketa1;
    	
    	mysocketa1 = socket( AF_INET , SOCK_STREAM , IPPROTO_TCP );
    	
    	SOCKADDR_IN serverInfo;
    
    	serverInfo.sin_family = AF_INET;
    	serverInfo.sin_port = htons(8888);
    	serverInfo.sin_addr.s_addr = inet_addr("10.1.50.30");
    	
    	ZeroMemory(buffer, 256);
    	
    	while (!key[KEY_ESC])
    	{
              tempstore = getkeypress();
              switch(tempstore)
              {
                            case 1:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "a" );
                                 buffer[bufnum]='a';
                                 bufnum++;
                                 tx = tx + tspacing;
                                 break;
                            case 2:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "b" );
                                 tx = tx + tspacing;
                                 break;
                            case 3:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "c" );
                                 tx = tx + tspacing;
                                 break;
                            case 4:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "d" );
                                 tx = tx + tspacing;
                                 break;
                            case 5:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "e" );
                                 tx = tx + tspacing;
                                 break;
                            case 6:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "f" );
                                 tx = tx + tspacing;
                                 break;
                            case 7:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "g" );
                                 tx = tx + tspacing;
                                 break;
                            case 8:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "h" );
                                 tx = tx + tspacing;
                                 break;
                            case 9:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "i" );
                                 tx = tx + tspacing;
                                 break;
                            case 10:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "j" );
                                 tx = tx + tspacing;
                                 break;
                            case 11:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "k" );
                                 tx = tx + tspacing;
                                 break;
                            case 12:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "l" );
                                 tx = tx + tspacing;
                                 break;
                            case 13:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "m" );
                                 tx = tx + tspacing;
                                 break;
                            case 14:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "n" );
                                 tx = tx + tspacing;
                                 break;
                            case 15:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "o" );
                                 tx = tx + tspacing;
                                 break;
                            case 16:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "p" );
                                 tx = tx + tspacing;
                                 break;
                            case 17:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "q" );
                                 tx = tx + tspacing;
                                 break;
                            case 18:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "r" );
                                 tx = tx + tspacing;
                                 break;
                            case 19:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "s" );
                                 tx = tx + tspacing;
                                 break;
                            case 20:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "t" );
                                 tx = tx + tspacing;
                                 break;
                            case 21:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "u" );
                                 tx = tx + tspacing;
                                 break;
                            case 22:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "v" );
                                 tx = tx + tspacing;
                                 break;
                            case 23:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "w" );
                                 tx = tx + tspacing;
                                 break;
                            case 24:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "x" );
                                 tx = tx + tspacing;
                                 break;
                            case 25:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "y" );
                                 tx = tx + tspacing;
                                 break;
                            case 26:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "z" );
                                 tx = tx + tspacing;
                                 break;
                            case 97:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , "!" );
                                 tx = tx + tspacing;
                                 break;
                            case 98:
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , " " );
                                 tx = tx + tspacing;
                                 break;
                            case 99:
                                 tx = tx - tspacing;
                                 textprintf( screen , font , tx , ty , makecol(255,255,255) , " " );
                                 break;
                            case 100:
                                 //enter code for "ENTER" here
                                 //tx = tx - tspacing;
                                 //textprintf( screen , font , tx , ty , makecol(255,255,255) , " " );
                                 break;
                                 }
        };
        textprintf( screen , font , 10 , 30 , makecol(255,255,255) , "reached this point" );
        textprintf( screen , font , 10 , 50 , makecol(255,255,255) , "%s" , buffer);
        
        while(!key[KEY_ENTER]){};
    	
    	connect(mysocketa1, (LPSOCKADDR)&serverInfo , sizeof(struct sockaddr));
        
        send( mysocketa1 , buffer , strlen(buffer) , 0 );	
    	
    	
    	closesocket(mysocketa1);
    	
    	WSACleanup();
    	
    	allegro_exit();	
    }
    END_OF_MAIN()
    tried many methods, including ssharish's method. but cant get it to work.

    thanks
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There's an example here that might help:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    (look at the second block of code)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int bufnum = 1;
    char arrays start at 0.
    int bufnum = 0;

  9. #9
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    awesome, it was that. thanks alot, sometimes you just need a fresh mind to look at the code. i completely overlooked that.

    thanks
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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