开发者

change the data of char array

开发者 https://www.devze.com 2023-01-02 14:18 出处:网络
i have an array of type char char *arg[100] this array its reading the data from somewhere, when that when i print arg[0], it prints the value \"help\", and when i print the arg[1], it prints the v

i have an array of type char

char *arg[100]

this array its reading the data from somewhere, when that when i print arg[0], it prints the value "help", and when i print the arg[1], it prints the value "exit".. as an example

what i want exactly is to change the arg[0] from "help" to anything else, like "man"

how i can do that???

thank you

the code:

int executeCommands(char arg31[])
{       
    pid_t pid;
    int status,number; 
    char *arg3[10]; 
    //char str2[21]; 
    x = 0;
    arg3[x] = strtok(arg31, " \n\t");//this line to tokenize every commands and its arguments from the                          passed array of chars (which is the command)
    while(arg3[x])
    arg3[++x] = strtok(NULL, " \n\t");

    if(NULL!=arg3[0])
    {

        if(strcasecmp(arg3[0],"cat")==0) //done
        {
            int f=0,n;
            char l[1];
            struct stat s;
            if(x!=2)
            {
                    printf("Mismatch argument\n");
                return 0;

            }
else if(strcmp(arg3[0],"help")==0) // done 
        {

            if (strcmp(arg3[1],"cat")==0)
                printf("1");
            else if(strcmp(arg3[1],"rm")==0)
                printf("1");
            else if(strcmp(arg3[1],"rmdir")==0)
                printf("1");
            else if(strcmp(arg3[1],"ls")==0)
                printf("1");
            else if(strcmp(arg3[1],"cp")==0)
                printf("1");
            else if(strcmp(arg3[1],"mv")==0)
        开发者_StackOverflow        printf("1");
            else if(strcmp(arg3[1],"hi")==0)
                printf("1");
            else if(strcmp(arg3[1],"exit1")==0)
                printf("1");
            else if(strcmp(arg3[1],"sleep")==0)
                printf("1");
            else if(strcmp(arg3[1],"history")==0)
                printf("1");
            else if(strcmp(arg3[1],"type")==0)
                printf("1");
            else
            {   char manarg[] = "man\t";
                arg3[0] = strtok(manarg, " \n\t");
                executeCommands(arg3);
            }
            writeHistory(arg3);
        } 


this should put something else in :-

strcpy(arg3[0], "blah");

boiling your function right down, this compiled just fine for me :-

int executeCommands(char arg31[])
{           
    char *arg3[10]; 

    arg3[0] = arg31;

    strcpy(arg3[0], "blah");
    return 0;
}


So the array points to tokens in another string. You should not copy a new value into that buffer because the new value may be longer than the old value and you don't want to overwrite the next token.

If the new value will be a literal, like "man" you can just do this:

arg3[0] = "man";

If the new value will be variable string, then something like this:

char newToken[64];
newToken[sizeof(newToken)-1] = 0;
strncpy(newToken, "whatever", sizeof(newToken)-1);
arg3[0] = newToken;


Propably you have char *arg[100];

You can do this with:

strncpy(arg[0], "man", sizeof(256));

Where 256 is number of bytes allocated on memory behind this pointer. I think you have other value of allocated bytes.

You can do this with this code too:

arg[0] = (char*)"man";

But in second example you do not good conversion from const char* to char*

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号