problem copy from array to another

This is a discussion on problem copy from array to another within the C Programming forums, part of the General Programming Boards category; I've got this problem: I have two arrays. The first is just a char array that contains for example this: ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    40

    problem copy from array to another

    I've got this problem:
    I have two arrays.
    The first is just a char array that contains for example this:
    Code:
    !cmd
    !cmd2
    !cmd3 paramater
    !cmd4
    And I need to get it into an array of a struct. Struct is as follows:
    Code:
    typedef struct {
    	char	*cmd;
    	char	*para;
    } g_acl_Commands_t;
    If I would fill it from the code, I would do this:
    Code:
    static g_acl_Commands_t	aclCommands[] =
    {
    { "!cmd", '' },
    { "!cmd2", '' },
    { "!cmd3", "testcode" },
    { "!cmd4", '' },
    };
    But it needs to be done in runtime, and the first array can change.
    So I determine the first position of the !, and then the first coming newline, string terminator or space, then I have a starting point and an ending point which I need to copy, but now the problem is, how do you do that? :P
    I tried with a for loop:
    Code:
    b = 0;
    for (a = startpoint; a < endpoint; a++)
    {
          temparray[b] = firstarray[a]
          b++;
    }
    then I would copy the temparray to the array of that struct, and then repeat the whole thing every time I have found new points, until there are no more, and then I should have an array of that struct filled with the right data, but it doesn't work, so how do I do that?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Describe what doesn't work - doesn't work can range from "you get one extra character in your array at times" to "the application crashes out with a segfault".

    I don't see anything directly wrong with your approach as such. By the way, if your struct has char *cmd, then you could actually do a different thing: malloc some memory for your new string, and do "cmd = newstring" - in fact, it's probably not valid to overwrite the current content of "cmd" as if it was an array, and you'd absolutely be in trouble if the new string is longer than the original one. The reason you probably can't overwrite the original string is that string literals (that is, text-strings in your code) are in "const" memory, meaning that they can not be written to - the application will exit with an error (segfault or similar).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Sounds like you need to do some string parsing to me.
    From the example you gave, it looks like you just need to look for a space in the string, and if it's there, put what's before the space in the cmd string and the rest in the parameter string.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    40
    the first problem I encountered was it was crashing, but I found a way. But now I get wrong data,
    the declarations before the function:
    Code:
    static char filetext[10000];
    typedef struct {
    	char	*cmd;
    	char	*code;
    } g_acl_Commands_t;
    
    static g_acl_Commands_t	*aclCommands;
    current code:
    Code:
    void g_acl_LoadCommands(void)
    {
    	int	a, b, c, len, row, startpoint, endpoint, endfilled;
    	
    	g_acl_ReadFile("commands.cfg"); //this reads a file and fills filetext with the text
    	trap_Printf(filetext); //this outputs filetext, to check if it worked fine, and it is
    	len++;
                    row = 0;
    	for (a = 0; a <= strlen(filetext); a++)
    	{
    		if (filetext[a] == '\n')
    			len++;
    	}
    	len++;
    	aclCommands = malloc(len);
    	for (a = 0; a <= strlen(filetext); a++)
    	{
    		if (filetext[a] == '!')
    		{
    			trap_Printf("found a !!\n");
    			startpoint = a;
    			endfilled = 0;
    			for (b = startpoint; b <= strlen(filetext); b++)
    			{
    				if (((filetext[b] == ' ') || (filetext[b] == '\n') || (filetext[b] == '\0')) && (endfilled == 0))
    				{
    					endpoint = b;
    					endfilled = 1;
    				}
    			}
    			c = 0;
    			for (b = startpoint; b < endpoint; b++)
    			{
    				
    				aclCommands[row].cmd[c] = filetext[b];
    				c++;
    			}
    			aclCommands[row].cmd[c] = 0;
    			if (filetext[endpoint] == ' ')
    			{
    				startpoint = endpoint + 1;
    				endfilled = 0;
    				for (b = startpoint; b <= strlen(filetext); b++)
    				{
    					if (((filetext[b] == '\n') || (filetext[b] == '\0')) && (endfilled == 0))
    					{
    						endpoint = b;
    						endfilled = 1;
    					}
    				}
    				c = 0;
    				for (b = startpoint; b < endpoint; b++)
    				{
    					aclCommands[row].code[c] = filetext[b];
    					
    					c++;
    				}
    				aclCommands[row].code[c] = 0;
    			}
    			row++;
    			a = endpoint;
    		}
    	}
    
    	trap_Printf("end loadcommands\n");
    }
    but the output in the array is doing someting weird, so can anyone verify that my code hasn't got any weird twists (because i'm pretty amature )
    but a part of it is right, but not all of it
    Last edited by s-men; 09-07-2007 at 01:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-10-2008, 10:29 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. Help to copy Array a into first portion of array b
    By Anna Lane in forum C Programming
    Replies: 4
    Last Post: 11-25-2002, 08:38 PM

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