need help, going mad about array pointers and strings...
Hi,
I have a problem and I am very close to going crazy. If someone could help me, I would be appreciate...
struct command_line
{
char *command_name;
char **args;
};
main ()
{
struct command_line *command;
command=(struct command_line *)malloc(sizeof (struct command_line);
char real_command_line[500];
...
...
i=0,j=0, k=0;
command->args=malloc((n+1) * sizeof(char *));
//assume real_command_line has "please be done" in it. n=3, and the allocation for args will be 4.
//the below parts takes "please", copies it in a string named buffer, equals buffer to command->args[k], then takes "be" and again the same and "done" and the same operations are done. but command->args[k] has a null value everytime and I can't solve it!
while(real_command_line[i]!='\0')
{
if(real_command_line[i]==' ' || real_command_line[i]=='\0' )
{
buffer[j]='\0'; j=0;
command->args[k]=buffer;
i++; printf("buffer %s\n", command->args[k] );
k++;
}
buffer[j]=real_command_line[i];
i++, j++;
if(real_command_line[i]=='\0')
{
buffer[j]='\0'; j=0;
command->args[k]=buffer;
printf("buffer %s\n", command->args[k]);
k++;
}
}
command->args[k]=0;
printf results are ok.
but after this step, when I try to printf command->args[0],
it gives the last string which is "done". Actually all command->args[0], command->args[1], command->args[2] is getting equal to "done". I don't know why and how can I solve this...
Last edited by haydari; 04-12-2007 at 04:06 PM.
|