我正在开发我的第一个 C 程序,它需要根据需要构造和解构字符串。不过,我不知道如何正确地做到这一点。有人可以解释一下吗?
据我所知,这是我的代码:
int main( int argc, char ** argv )
{
if ( argc != 2 )
{
_print_help(); /* defined above main() */
exit(0);
}
char *s = argv[1];
char *val = "";
register int i; /* no idea why I need "register" and not just int */
for(i=0;s[i];++i) /* loop over the string, take each character
{ and add it to another string */
strcat(val,s[i]);
printf("v:%s;\n",val);
}
printf("expression: %s\n",s);
}
gcc 从 make 文件中调用(使用 autotools 生成):
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT c-calc.o -MD -MP -MF .deps/c-calc.Tpo -c -o c-calc.o c-calc.c
现在它会发出警告,我不应该将整数作为 strcat 的第二个输入,并且 string3.h 期望const char * __restrict__
而不仅仅是 a char
(我必须翻译它们,因为我的 Ubuntu 用德语与我交谈)。激活我编译的程序会抛出一个Memoryaccess error
,然后程序返回139
似乎意味着Segmentatation 违规的代码。