需求 保存一个未被更改前的command 到tmp 在改后把tmp的内容赋值给command
学习内容 : 指针相关的内存 指向, 是不是野指针,是不是指向command,有没有malloc?
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
char *getfile(char *str)
{
char *tmp = str;
char *p;
p = strtok(tmp, " ");
p = strtok(NULL, " ");
return p;
}
int main()
{
char command[2048]= "jianglongjie henshuai!";
char *tmp = NULL ;
tmp = (char *)malloc(2048);
strcpy(tmp,command);
printf("tmp:%s \n",tmp);
// printf("tmp:%s \n",tmp);
char *file = getfile(command);
//i memset(command,0,sizeof(command));
// strcpy(command,tmp);
printf("cpy前 tmp:%s \n",tmp);
strcpy(command,tmp); // 因为command改变, 我想从我保存的tmp 赋值给改变后的command
printf("cpy后 tmp:%s \n",tmp);
printf("command:%s \n",command);
printf("file %s \n",file);
// printf("tmp:%s \n",tmp);
return 0;
}