views:

126

answers:

3

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)
                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);
        } 
A: 

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*

Svisstack
i tried it it, its not working.. the error is strncpy(arg[0], "man", sizeof(256));
lele Rom
the error is expected ‘char *’ but argument is of type ‘char **’
lele Rom
hmm, your declaration is char *arg[100]; ?
Svisstack
the second solution gave me this error./Rshell.c:377: error: ‘reinterpret_cast’ undeclared (first use in this function)./Rshell.c:377: error: (Each undeclared identifier is reported only once./Rshell.c:377: error: for each function it appears in.)./Rshell.c:377: error: expected expression before ‘char’
lele Rom
can i use strtok(arg31, " \n\t")?? where arg31 is another char array?
lele Rom
what exactly is the declaration of arg[0]?
Keith Nicholas
@lele: The second example is C++ only.
Yann Ramin
err whats the declaration of arg?
Keith Nicholas
okey am going to post the code, just a min
lele Rom
A: 

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;
}
Keith Nicholas
note: expected ‘char *’ but argument is of type ‘char **’this is the error i get :S
lele Rom
arg3 is a char** arg3[0] is a char* and arg3[0][0] is a char
Keith Nicholas
the error is at line 120, which is the line of declaring the executeCommands function..
lele Rom
oh, what are you passing to excecuatecommands? are you passing argv to it? because that would get your error too
Keith Nicholas
that is the error, that am passing the wrong arg, so i should create another char Char arg[] not a char *arg[]... right??
lele Rom
hmmmm, I think you need to understand pointers and pointers to pointers....I think it would help you work through this problem. http://computer.howstuffworks.com/c32.htm
Keith Nicholas
actually that helped the problem, but now i am facing another problem, i will try to solve, thanks everyone :D
lele Rom
A: 

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;
Amardeep
actually both ways it gives me the same errornote: expected ‘char *’ but argument is of type ‘char **’
lele Rom
Are you assigning to arg31 or arg3? You cannot do that to arg31, it is a char array. arg3 is an array of pointers and it will work with that.
Amardeep
am doing that for arg 3
lele Rom
it sounds like you are passing the wrong thing in. like you are doing... strcpy(arg3, "blah"); that would get the error you are talking about.....
Keith Nicholas
If you are using arg3, you MUST have an index to dereference. It must be arg3[0], not arg3 alone.
Amardeep