关于LINUX编程 char *value = argv[1]; 导致段错误 (核心已转储)问题
其他问答
1
编译环境: UBUNTU下自带的GCC
执行:
$ ./test 1 sky 123
输出:
a:K:sky,V:123 段错误 (核心已转储)
PLAN b输出无误。
PLAN a下面的 method A、B都不行,反馈都是 段错误 (核心已转储)
请问大佬们知道这个错误的原因在哪儿吗?
#include <stdio.h>
#include <string.h>
int main (int argc,char** argv)
{
#if 1 //PLAN a
//method A
char* key,value;
printf("a:K:%s,V:%s\r\n",argv[2],argv[3]);
key = argv[2];
value = argv[3];
//method B
char *key,*value;
printf("a:K:%s,V:%s\r\n",argv[2],argv[3]);
strcpy(key,argv[2]);
strcpy(value,argv[3]);
printf("b:K:%s,V:%s\r\n",key,value);
#else //PLAN b
char key[16],value[16];
printf("a:K:%s,V:%s\r\n",argv[2],argv[3]);
strcpy(key,argv[2]);
strcpy(value,argv[3]);
printf("b:K:%s,V:%s\r\n",key,value);
#endif
return 0;
}
-
#include <stdio.h> #include <stdlib.h> #include <string.h> int main (int argc,char** argv) { #if 1 //PLAN a char *key,*value; printf("a:K:%s,V:%s\r\n",argv[2],argv[3]); //method A key = argv[2]; value = argv[3]; //method B key = malloc(16); value = malloc(16); strcpy(key,argv[2]); strcpy(value,argv[3]); printf("b:K:%s,V:%s\r\n",key,value); #else //PLAN b char key[16],value[16]; printf("a:K:%s,V:%s\r\n",argv[2],argv[3]); strcpy(key,argv[2]); strcpy(value,argv[3]); printf("b:K:%s,V:%s\r\n",key,value); #endif return 0; }
-
char* key,value
;你的value是char而不是char *
发表回复