我试图在C中编写一个程序,根据给定的命令行参数以两种不同的方式将2个字符串合并在一起。
' -i'合并字符串交替,像这样
combine_strings -i 请输入最多30个字符的字符串:abcde 请输入最多30个字符的字符串:1234567 组合字符串是:a1b2c3d4e567
' -w'将两个字符串连接在一起,并插入一个' *'在字符串的每个字符之间,像这样
combine_strings -w 请输入最多30个字符的字符串:abcde 请输入最多30个字符的字符串:1234567 组合字符串为:a b c d e * 1 * 2 * 3 * 4 * 5 * 6 * 7
这是我到目前为止所得到的。
#include<stdio.h>
#include<string.h>
#include<memory.h>
void alt_merge(char *string1,char *string2,char *stringfinal){
while (*string1 != '\0' && *string2 != '\0')
{
*stringfinal++= *string1++;
*stringfinal++ = *string2++;
}
while (*string1 != '\0')
*stringfinal++=*string1++;
while (*string2 != '\0')
*stringfinal++ = *string2++;
*stringfinal='\0';
}
void concate_star(char *string1,char *string2,char *stringfinal){
strcpy(stringfinal,strcat(string1,string2));
}
int main(int argc,char *argv[]){
char *string1[30+1];
char *string2[30+1];
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
if(argv[1]=='-i'){
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string1);
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string2);
alt_merge(string1,string2,stringfinal);
printf("The combined string is: %s",stringfinal);
}
else if(argv[1]=='-w'){
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string1);
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string2);
concate_star(*string1,*string2,*stringfinal);
printf("The combined string is: %s",stringfinal);
}
return 0;
}
我收到这些错误消息。
jdoodle.c: In function 'main':
jdoodle.c:27:23: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~
jdoodle.c:27:23: warning: incompatible implicit declaration of built-in function 'malloc'
jdoodle.c:27:23: note: include '<stdlib.h>' or provide a declaration of 'malloc'
jdoodle.c:27:37: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~~
In file included from jdoodle.c:2:0:
/usr/include/string.h:384:15: note: expected 'const char *' but argument is of type 'char **'
extern size_t strlen (const char *__s)
^~~~~~
jdoodle.c:27:53: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~~
In file included from jdoodle.c:2:0:
/usr/include/string.h:384:15: note: expected 'const char *' but argument is of type 'char **'
extern size_t strlen (const char *__s)
^~~~~~
jdoodle.c:29:17: warning: multi-character character constant [-Wmultichar]
if(argv[1]=='-i'){
^~~~
jdoodle.c:29:15: warning: comparison between pointer and integer
if(argv[1]=='-i'){
^~
jdoodle.c:35:23: warning: passing argument 1 of 'alt_merge' from incompatible pointer type [-Wincompatible-pointer-types]
alt_merge(string1,string2,stringfinal);
^~~~~~~
jdoodle.c:5:6: note: expected 'char *' but argument is of type 'char **'
void alt_merge(char *string1,char *string2,char *stringfinal){
^~~~~~~~~
jdoodle.c:35:31: warning: passing argument 2 of 'alt_merge' from incompatible pointer type [-Wincompatible-pointer-types]
alt_merge(string1,string2,stringfinal);
^~~~~~~
jdoodle.c:5:6: note: expected 'char *' but argument is of type 'char **'
void alt_merge(char *string1,char *string2,char *stringfinal){
^~~~~~~~~
jdoodle.c:39:22: warning: multi-character character constant [-Wmultichar]
else if(argv[1]=='-w'){
^~~~
jdoodle.c:39:20: warning: comparison between pointer and integer
else if(argv[1]=='-w'){
^~
jdoodle.c:45:44: warning: passing argument 3 of 'concate_star' makes pointer from integer without a cast [-Wint-conversion]
concate_star(*string1,*string2,*stringfinal);
^
jdoodle.c:19:6: note: expected 'char *' but argument is of type 'char'
void concate_star(char *string1,char *string2,char *stringfinal){
^~~~~~~~~~~~
我是编程新手,所以感谢任何帮助和建议。
答案 0 :(得分:0)
有两个问题,你没有stdlib.h
包含函数malloc()
的声明。
此外,您将错误的参数传递给strlen()
,但您通过了char**
。在其他功能中也是如此。您的参数不匹配。
char *string1[30+1];
char *string2[30+1];
你想要
char string1[30+1];
char string2[30+1];
在将strcmp
与"-i"
进行比较时,您应该使用argv[1]
。
现在argv
基本上是char*
的数组。您基本上将空终止的char数组与(string)与"-i"
进行比较。 (不是'-i'
)。
还要注意一件有趣的事情,
编译说
jdoodle.c:29:15: warning: comparison between pointer and integer
if(argv[1]=='-i'){
^~
包含多个c-char的普通字符文字是a 多字形文字。多字符文字的类型为int和 实现定义的价值。
'-i'
是一个多字符文字。这个类型为int
,这就是pointer
和int
之间警告比较的原因。
还有一件事你应该清楚地知道,当你将一个数组传递给一个函数时,它会衰减成指向数组第一个元素的指针。
char *s[100]
衰减到char**
,因为数组的内容为char*
,指向它的指针将为char**
。